-
Notifications
You must be signed in to change notification settings - Fork 1
/
cafiles.go
186 lines (164 loc) · 5.2 KB
/
cafiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// cafiles can fetch CA certificate and private key PEM files from many storage backends.
// PEM encoded CA files can be fetched from local filesystem, AWS S3, or AWS Secrets Manager.
package cafiles
import (
"context"
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/url"
"os"
"time"
"github.com/RealImage/bifrost"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
const fetchTimeout = time.Minute
// GetCertificate returns a namespace and a bifrost certificate from uri.
// uri can be a relative or absolute file path, file://... uri, s3://... uri,
// or an AWS S3 or AWS Secrets Manager ARN.
// The certificate is validated before returning.
func GetCertificate(ctx context.Context, uri string) (*bifrost.Certificate, error) {
ctx, cancel := context.WithTimeout(ctx, fetchTimeout)
defer cancel()
certPem, err := getPemFile(ctx, uri)
if err != nil {
return nil, fmt.Errorf("error getting file %s: %w", uri, err)
}
cert, err := bifrost.ParseCertificate(certPem.Bytes)
if err != nil {
return nil, fmt.Errorf("error validating certificate: %w", err)
}
return cert, nil
}
// GetPrivateKey retrieves a PEM encoded private key from uri.
// uri can be one of a relative or absolute file path, file://... uri, s3://... uri,
// or an AWS S3 or AWS Secrets Manager ARN.
func GetPrivateKey(ctx context.Context, uri string) (*ecdsa.PrivateKey, error) {
ctx, cancel := context.WithTimeout(ctx, fetchTimeout)
defer cancel()
keyPem, err := getPemFile(ctx, uri)
if err != nil {
return nil, fmt.Errorf("error getting file %s: %w", uri, err)
}
var key *ecdsa.PrivateKey
switch blockType := keyPem.Type; blockType {
case "PRIVATE KEY":
k, err := x509.ParsePKCS8PrivateKey(keyPem.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing key uri %s: %w", uri, err)
}
key, ok := k.(*ecdsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("unexpected key type: %T", key)
}
case "EC PRIVATE KEY":
if key, err = x509.ParseECPrivateKey(keyPem.Bytes); err != nil {
return nil, fmt.Errorf("error parsing key uri %s: %w", uri, err)
}
}
return key, nil
}
func getPemFile(ctx context.Context, uri string) (*pem.Block, error) {
url, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("error parsing file uri %w", err)
}
var pemData []byte
switch s := url.Scheme; s {
case "s3":
pemData, err = getS3Key(ctx, url.Host, url.Path[1:])
case "arn":
// s3 and secretsmanager arns are supported
parsedArn, err := arn.Parse(uri)
if err != nil {
return nil, fmt.Errorf("error parsing arn %w", err)
}
switch svc := parsedArn.Service; svc {
case "s3":
pemData, err = getS3Key(ctx, url.Host, url.Path[1:])
case "secretsmanager":
pemData, err = getSecret(ctx, uri)
default:
return nil, fmt.Errorf("cannot load pem file from %s", svc)
}
if err != nil {
return nil, fmt.Errorf("error fetching pem file: %w", err)
}
case "", "file":
pemData, err = os.ReadFile(url.Path)
default:
return nil, fmt.Errorf("unsupported uri scheme %s", s)
}
if err != nil {
return nil, fmt.Errorf("error fetching pem file: %w", err)
}
block, _ := pem.Decode(pemData)
if block == nil {
return nil, fmt.Errorf("no pem data found")
}
return block, nil
}
func getS3Key(ctx context.Context, bucket, key string) ([]byte, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
rawObject, err := s3.New(sess).GetObjectWithContext(
ctx,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, fmt.Errorf("error getting s3 object: %w", err)
}
body, err := io.ReadAll(rawObject.Body)
if err != nil {
return nil, err
}
return body, nil
}
func getSecret(ctx context.Context, secretARN string) ([]byte, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
input := secretsmanager.GetSecretValueInput{SecretId: aws.String(secretARN)}
val, err := secretsmanager.New(sess).GetSecretValueWithContext(ctx, &input)
if err != nil {
return nil, err
}
if val.SecretBinary != nil {
return val.SecretBinary, nil
}
if val.SecretString != nil {
return []byte(*val.SecretString), nil
}
return nil, fmt.Errorf("no secret data found")
}
// GetCertKey returns a bifrost certificate and private key from certUri and keyUri.
func GetCertKey(
ctx context.Context,
certUri string,
keyUri string,
) (*bifrost.Certificate, *ecdsa.PrivateKey, error) {
cert, err := GetCertificate(ctx, certUri)
if err != nil {
return nil, nil, fmt.Errorf("error getting cert: %w", err)
}
key, err := GetPrivateKey(ctx, keyUri)
if err != nil {
return nil, nil, fmt.Errorf("error getting key: %w", err)
}
if cert.PublicKey.X.Cmp(key.X) != 0 || cert.PublicKey.Y.Cmp(key.Y) != 0 {
return nil, nil, fmt.Errorf("certificate and key do not match")
}
return cert, key, nil
}