Debian Codegraph @

azure/azuredb/azuredb.go

raw
1 // Copyright 2019 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 azuredb contains Wire providers that are common across Azure Database.
16 package azuredb
17
18 import (
19 "context"
20 "crypto/x509"
21 "encoding/pem"
22 "fmt"
23 "io"
24 "net/http"
25
26 "golang.org/x/net/context/ctxhttp"
27 )
28
29 const caBundleURL = "https://www.digicert.com/CACerts/BaltimoreCyberTrustRoot.crt.pem"
30
31 // A CertPoolProvider returns a certificate pool that contains the Azure CA certificate.
32 type CertPoolProvider interface {
33 AzureCertPool(context.Context) (*x509.CertPool, error)
34 }
35
36 // CertFetcher pulls the Azure CA certificates from Digicert's servers. The zero
37 // value will fetch certificates using the default HTTP client.
38 type CertFetcher struct {
39 // Client is the HTTP client used to make requests. If nil, then
40 // http.DefaultClient is used.
41 Client *http.Client
42 }
43
44 // AzureCertPool fetches the Azure CA certificates and places them into a pool.
45 // It is safe to call from multiple goroutines.
46 func (cf *CertFetcher) AzureCertPool(ctx context.Context) (*x509.CertPool, error) {
47 certs, err := cf.Fetch(ctx)
48 if err != nil {
49 return nil, err
50 }
51 certPool := x509.NewCertPool()
52 for _, c := range certs {
53 certPool.AddCert(c)
54 }
55 return certPool, nil
56 }
57
58 // Fetch fetches the Azure CA certificates. It is safe to call from multiple goroutines.
59 func (cf *CertFetcher) Fetch(ctx context.Context) ([]*x509.Certificate, error) {
60 client := cf.Client
61 if client == nil {
62 client = http.DefaultClient
63 }
64 resp, err := ctxhttp.Get(ctx, client, caBundleURL)
65 if err != nil {
66 return nil, fmt.Errorf("fetch Azure certificates: %v", err)
67 }
68 defer resp.Body.Close()
69 if resp.StatusCode != http.StatusOK {
70 return nil, fmt.Errorf("fetch Azure certificates: HTTP %s", resp.Status)
71 }
72 pemData, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1 << 20}) // limit to 1MiB
73 if err != nil {
74 return nil, fmt.Errorf("fetch Azure certificates: %v", err)
75 }
76 var certs []*x509.Certificate
77 for len(pemData) > 0 {
78 var block *pem.Block
79 block, pemData = pem.Decode(pemData)
80 if block == nil {
81 break
82 }
83 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
84 continue
85 }
86 c, err := x509.ParseCertificate(block.Bytes)
87 if err != nil {
88 return nil, fmt.Errorf("fetch Azure certificates: %v", err)
89 }
90 certs = append(certs, c)
91 }
92 return certs, nil
93 }