| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package rds contains Wire providers that are common across RDS. |
| 16 |
package rds // import "gocloud.dev/aws/rds" |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"crypto/x509" |
| 21 |
"encoding/pem" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"net/http" |
| 25 |
|
| 26 |
"github.com/google/wire" |
| 27 |
"golang.org/x/net/context/ctxhttp" |
| 28 |
) |
| 29 |
|
| 30 |
// CertFetcherSet is a Wire provider set that provides the RDS certificate pool |
| 31 |
// by pulling from Amazon's servers. |
| 32 |
var CertFetcherSet = wire.NewSet( |
| 33 |
wire.Struct(new(CertFetcher), "Client"), |
| 34 |
wire.Bind(new(CertPoolProvider), new(*CertFetcher)), |
| 35 |
) |
| 36 |
|
| 37 |
// A CertPoolProvider obtains a certificate pool that contains the RDS CA certificate. |
| 38 |
type CertPoolProvider interface { |
| 39 |
RDSCertPool(context.Context) (*x509.CertPool, error) |
| 40 |
} |
| 41 |
|
| 42 |
// caBundleURL is the URL to the public RDS Certificate Authority keys. |
| 43 |
const caBundleURL = "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem" |
| 44 |
|
| 45 |
// CertFetcher pulls the RDS CA certificates from Amazon's servers. The zero |
| 46 |
// value will fetch certificates using the default HTTP client. |
| 47 |
type CertFetcher struct { |
| 48 |
// Client is the HTTP client used to make requests. If nil, then |
| 49 |
// http.DefaultClient is used. |
| 50 |
Client *http.Client |
| 51 |
} |
| 52 |
|
| 53 |
// RDSCertPool fetches the RDS CA certificates and places them into a pool. |
| 54 |
// It is safe to call from multiple goroutines. |
| 55 |
func (cf *CertFetcher) RDSCertPool(ctx context.Context) (*x509.CertPool, error) { |
| 56 |
certs, err := cf.Fetch(ctx) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
certPool := x509.NewCertPool() |
| 61 |
for _, c := range certs { |
| 62 |
certPool.AddCert(c) |
| 63 |
} |
| 64 |
return certPool, nil |
| 65 |
} |
| 66 |
|
| 67 |
// Fetch fetches the RDS CA certificates. It is safe to call from multiple goroutines. |
| 68 |
func (cf *CertFetcher) Fetch(ctx context.Context) ([]*x509.Certificate, error) { |
| 69 |
client := cf.Client |
| 70 |
if client == nil { |
| 71 |
client = http.DefaultClient |
| 72 |
} |
| 73 |
resp, err := ctxhttp.Get(ctx, client, caBundleURL) |
| 74 |
if err != nil { |
| 75 |
return nil, fmt.Errorf("fetch RDS certificates: %v", err) |
| 76 |
} |
| 77 |
defer resp.Body.Close() |
| 78 |
if resp.StatusCode != http.StatusOK { |
| 79 |
return nil, fmt.Errorf("fetch RDS certificates: HTTP %s", resp.Status) |
| 80 |
} |
| 81 |
pemData, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB |
| 82 |
if err != nil { |
| 83 |
return nil, fmt.Errorf("fetch RDS certificates: %v", err) |
| 84 |
} |
| 85 |
var certs []*x509.Certificate |
| 86 |
for len(pemData) > 0 { |
| 87 |
var block *pem.Block |
| 88 |
block, pemData = pem.Decode(pemData) |
| 89 |
if block == nil { |
| 90 |
break |
| 91 |
} |
| 92 |
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { |
| 93 |
continue |
| 94 |
} |
| 95 |
c, err := x509.ParseCertificate(block.Bytes) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("fetch RDS certificates: %v", err) |
| 98 |
} |
| 99 |
certs = append(certs, c) |
| 100 |
} |
| 101 |
return certs, nil |
| 102 |
} |