| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package awskms |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"encoding/csv" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"regexp" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
|
| 27 |
"github.com/aws/aws-sdk-go-v2/config" |
| 28 |
"github.com/aws/aws-sdk-go-v2/credentials" |
| 29 |
"github.com/aws/aws-sdk-go-v2/service/kms" |
| 30 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 31 |
"github.com/tink-crypto/tink-go/v2/core/registry" |
| 32 |
"github.com/tink-crypto/tink-go/v2/tink" |
| 33 |
) |
| 34 |
|
| 35 |
const ( |
| 36 |
awsPrefix = "aws-kms://" |
| 37 |
) |
| 38 |
|
| 39 |
var ( |
| 40 |
errCred = errors.New("invalid credential path") |
| 41 |
errBadFile = errors.New("cannot open credential path") |
| 42 |
errCredCSV = errors.New("malformed credential CSV file") |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
type KMSAPI interface { |
| 47 |
Encrypt(ctx context.Context, params *kms.EncryptInput, optFns ...func(*kms.Options)) (*kms.EncryptOutput, error) |
| 48 |
Decrypt(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
type awsClient struct { |
| 54 |
keyURIPrefix string |
| 55 |
kms KMSAPI |
| 56 |
encryptionContextName EncryptionContextName |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
type ClientOption interface { |
| 62 |
set(ctx context.Context, a *awsClient) error |
| 63 |
} |
| 64 |
|
| 65 |
type option func(ctx context.Context, a *awsClient) error |
| 66 |
|
| 67 |
func (o option) set(ctx context.Context, a *awsClient) error { return o(ctx, a) } |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func WithCredentialPath(credentialPath string) ClientOption { |
| 78 |
return option(func(ctx context.Context, a *awsClient) error { |
| 79 |
if a.kms != nil { |
| 80 |
return errors.New("WithCredentialPath option cannot be used, KMS client already set") |
| 81 |
} |
| 82 |
|
| 83 |
cfg, err := getConfigFromCredentialPath(ctx, a.keyURIPrefix, credentialPath) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
a.kms = kms.NewFromConfig(cfg) |
| 88 |
return nil |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
func WithKMS(kms KMSAPI) ClientOption { |
| 99 |
return option(func(ctx context.Context, a *awsClient) error { |
| 100 |
if a.kms != nil { |
| 101 |
return errors.New("WithKMS option cannot be used, KMS client already set") |
| 102 |
} |
| 103 |
a.kms = kms |
| 104 |
return nil |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
type EncryptionContextName uint |
| 112 |
|
| 113 |
const ( |
| 114 |
|
| 115 |
AssociatedData EncryptionContextName = 1 + iota |
| 116 |
|
| 117 |
LegacyAdditionalData |
| 118 |
) |
| 119 |
|
| 120 |
var encryptionContextNames = map[EncryptionContextName]string{ |
| 121 |
AssociatedData: "associatedData", |
| 122 |
LegacyAdditionalData: "additionalData", |
| 123 |
} |
| 124 |
|
| 125 |
func (n EncryptionContextName) valid() bool { |
| 126 |
_, ok := encryptionContextNames[n] |
| 127 |
return ok |
| 128 |
} |
| 129 |
|
| 130 |
func (n EncryptionContextName) String() string { |
| 131 |
if !n.valid() { |
| 132 |
return "unrecognized value " + strconv.Itoa(int(n)) |
| 133 |
} |
| 134 |
return encryptionContextNames[n] |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func WithEncryptionContextName(name EncryptionContextName) ClientOption { |
| 147 |
return option(func(ctx context.Context, a *awsClient) error { |
| 148 |
if !name.valid() { |
| 149 |
return fmt.Errorf("invalid EncryptionContextName: %v", name) |
| 150 |
} |
| 151 |
if a.encryptionContextName != 0 { |
| 152 |
return errors.New("encryptionContextName already set") |
| 153 |
} |
| 154 |
a.encryptionContextName = name |
| 155 |
return nil |
| 156 |
}) |
| 157 |
} |
| 158 |
|
| 159 |
func newAWSClient(ctx context.Context, uriPrefix string, opts ...ClientOption) (*awsClient, error) { |
| 160 |
if !strings.HasPrefix(strings.ToLower(uriPrefix), awsPrefix) { |
| 161 |
return nil, fmt.Errorf("uriPrefix must start with %q, but got %q", awsPrefix, uriPrefix) |
| 162 |
} |
| 163 |
|
| 164 |
a := &awsClient{ |
| 165 |
keyURIPrefix: uriPrefix, |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
for _, opt := range opts { |
| 170 |
if err := opt.set(ctx, a); err != nil { |
| 171 |
return nil, fmt.Errorf("failed setting option: %v", err) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if a.kms == nil { |
| 177 |
k, err := getKMS(ctx, uriPrefix) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
a.kms = k |
| 182 |
} |
| 183 |
if a.encryptionContextName == 0 { |
| 184 |
a.encryptionContextName = AssociatedData |
| 185 |
} |
| 186 |
|
| 187 |
return a, nil |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
func NewClientWithOptions(ctx context.Context, uriPrefix string, opts ...ClientOption) (registry.KMSClient, error) { |
| 198 |
return newAWSClient(ctx, uriPrefix, opts...) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
func NewClient(ctx context.Context, uriPrefix string) (registry.KMSClient, error) { |
| 217 |
return NewClientWithOptions(ctx, uriPrefix, WithEncryptionContextName(LegacyAdditionalData)) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
func NewClientWithCredentials(ctx context.Context, uriPrefix string, credentialPath string) (registry.KMSClient, error) { |
| 242 |
return NewClientWithOptions(ctx, uriPrefix, WithCredentialPath(credentialPath), WithEncryptionContextName(LegacyAdditionalData)) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
func NewClientWithKMS(ctx context.Context, uriPrefix string, kms KMSAPI) (registry.KMSClient, error) { |
| 264 |
return NewClientWithOptions(ctx, uriPrefix, WithKMS(kms), WithEncryptionContextName(LegacyAdditionalData)) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
func (c *awsClient) Supported(keyURI string) bool { |
| 270 |
return strings.HasPrefix(keyURI, c.keyURIPrefix) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
func (c *awsClient) GetAEAD(keyURI string) (tink.AEAD, error) { |
| 282 |
if !c.Supported(keyURI) { |
| 283 |
return nil, fmt.Errorf("keyURI must start with prefix %s, but got %s", c.keyURIPrefix, keyURI) |
| 284 |
} |
| 285 |
|
| 286 |
keyID := strings.TrimPrefix(keyURI, awsPrefix) |
| 287 |
return newAWSAEAD(keyID, c.kms, c.encryptionContextName), nil |
| 288 |
} |
| 289 |
|
| 290 |
func getKMS(ctx context.Context, uriPrefix string) (*kms.Client, error) { |
| 291 |
r, err := getRegion(uriPrefix) |
| 292 |
if err != nil { |
| 293 |
return nil, err |
| 294 |
} |
| 295 |
|
| 296 |
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(r)) |
| 297 |
if err != nil { |
| 298 |
return nil, err |
| 299 |
} |
| 300 |
|
| 301 |
return kms.NewFromConfig(cfg), nil |
| 302 |
} |
| 303 |
|
| 304 |
func getConfigFromCredentialPath(ctx context.Context, uriPrefix string, credentialPath string) (aws.Config, error) { |
| 305 |
r, err := getRegion(uriPrefix) |
| 306 |
if err != nil { |
| 307 |
return aws.Config{}, err |
| 308 |
} |
| 309 |
|
| 310 |
if len(credentialPath) == 0 { |
| 311 |
return aws.Config{}, errCred |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
creds, err := extractCredsCSV(credentialPath) |
| 316 |
switch err { |
| 317 |
case nil: |
| 318 |
return config.LoadDefaultConfig(ctx, |
| 319 |
config.WithRegion(r), |
| 320 |
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{ |
| 321 |
Value: aws.Credentials{ |
| 322 |
AccessKeyID: creds.AccessKeyID, |
| 323 |
SecretAccessKey: creds.SecretAccessKey, |
| 324 |
}, |
| 325 |
}), |
| 326 |
) |
| 327 |
case errBadFile, errCredCSV: |
| 328 |
return aws.Config{}, err |
| 329 |
default: |
| 330 |
|
| 331 |
return config.LoadDefaultConfig(ctx, |
| 332 |
config.WithRegion(r), |
| 333 |
config.WithSharedConfigProfile("default"), |
| 334 |
config.WithSharedConfigFiles([]string{credentialPath}), |
| 335 |
) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
func extractCredsCSV(file string) (*aws.Credentials, error) { |
| 350 |
f, err := os.Open(file) |
| 351 |
if err != nil { |
| 352 |
return nil, errBadFile |
| 353 |
} |
| 354 |
defer f.Close() |
| 355 |
|
| 356 |
lines, err := csv.NewReader(f).ReadAll() |
| 357 |
if err != nil { |
| 358 |
return nil, err |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
if len(lines) > 0 && len(lines[0]) == 1 { |
| 364 |
return nil, errors.New("not a valid CSV credential file") |
| 365 |
} |
| 366 |
|
| 367 |
if len(lines) < 2 { |
| 368 |
return nil, errCredCSV |
| 369 |
} |
| 370 |
|
| 371 |
if len(lines[1]) < 4 { |
| 372 |
return nil, errCredCSV |
| 373 |
} |
| 374 |
|
| 375 |
return &aws.Credentials{ |
| 376 |
AccessKeyID: lines[1][2], |
| 377 |
SecretAccessKey: lines[1][3], |
| 378 |
}, |
| 379 |
nil |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
func getRegion(keyURI string) (string, error) { |
| 384 |
re1, err := regexp.Compile(`aws-kms://arn:(aws[a-zA-Z0-9-_]*):kms:([a-z0-9-]+):`) |
| 385 |
if err != nil { |
| 386 |
return "", err |
| 387 |
} |
| 388 |
r := re1.FindStringSubmatch(keyURI) |
| 389 |
if len(r) != 3 { |
| 390 |
return "", errors.New("extracting region from URI failed") |
| 391 |
} |
| 392 |
return r[2], nil |
| 393 |
} |