|
| 1 | +package credentialprovider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + "github.com/scality/mountpoint-s3-csi-driver/pkg/driver/node/envprovider" |
| 11 | + "google.golang.org/grpc/codes" |
| 12 | + "google.golang.org/grpc/status" |
| 13 | + "k8s.io/klog/v2" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // Keys expected in the Secret map from NodePublishVolumeRequest. |
| 18 | + keyID = "key_id" |
| 19 | + secretAccessKey = "access_key" |
| 20 | + |
| 21 | + // Upper limits (not exact) — suits Vault & test creds. |
| 22 | + maxAccessKeyIDLen = 16 |
| 23 | + maxSecretAccessKeyLen = 40 |
| 24 | +) |
| 25 | + |
| 26 | +/* |
| 27 | +Validation rules (loosened for cloudserver test credentials): |
| 28 | +
|
| 29 | + key_id – 1 … 16 chars, uppercase A–Z or 0–9 |
| 30 | + access_key – 1 … 40 chars, [A-Za-z0-9 / + =] |
| 31 | +
|
| 32 | +The patterns are supersets of AWS IAM and permit shorter dummy keys. |
| 33 | +*/ |
| 34 | +var ( |
| 35 | + accessKeyIDRe = regexp.MustCompile(`^[A-Z0-9]{1,` + strconv.Itoa(maxAccessKeyIDLen) + `}$`) |
| 36 | + secretAccessKeyRe = regexp.MustCompile(`^[A-Za-z0-9/+=]{1,` + strconv.Itoa(maxSecretAccessKeyLen) + `}$`) |
| 37 | +) |
| 38 | + |
| 39 | +// provideFromSecret validates credentials from a Kubernetes Secret. |
| 40 | +func (c *Provider) provideFromSecret(_ context.Context, provideCtx ProvideContext) (envprovider.Environment, error) { |
| 41 | + env := envprovider.Environment{} |
| 42 | + |
| 43 | + valid := map[string]struct{}{keyID: {}, secretAccessKey: {}} |
| 44 | + for k := range provideCtx.SecretData { |
| 45 | + if _, ok := valid[k]; !ok { |
| 46 | + klog.Warningf("credentialprovider: Secret contains unexpected key %q (ignored). Only %q and %q are supported.", |
| 47 | + k, keyID, secretAccessKey) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + id, okID := provideCtx.SecretData[keyID] |
| 52 | + sec, okSec := provideCtx.SecretData[secretAccessKey] |
| 53 | + |
| 54 | + if okID { |
| 55 | + id = strings.TrimSpace(id) |
| 56 | + if !accessKeyIDRe.MatchString(id) { |
| 57 | + klog.Warningf("credentialprovider: key_id %q is not uppercase alphanumeric or exceeds %d chars", |
| 58 | + id, maxAccessKeyIDLen) |
| 59 | + okID = false |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if okSec { |
| 64 | + sec = strings.TrimSpace(sec) |
| 65 | + if !secretAccessKeyRe.MatchString(sec) || !utf8.ValidString(sec) { |
| 66 | + klog.Warningf("credentialprovider: access_key is invalid or exceeds %d chars", |
| 67 | + maxSecretAccessKeyLen) |
| 68 | + okSec = false |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if okID && okSec { |
| 73 | + env.Set(envprovider.EnvAccessKeyID, id) |
| 74 | + env.Set(envprovider.EnvSecretAccessKey, sec) |
| 75 | + |
| 76 | + // FULL key_id logged (no masking) for audit purposes. |
| 77 | + klog.V(3).Infof("credentialprovider: volume %s authenticated with key_id %s", |
| 78 | + provideCtx.VolumeID, id) |
| 79 | + |
| 80 | + return env, nil |
| 81 | + } |
| 82 | + |
| 83 | + var missing []string |
| 84 | + if !okID { |
| 85 | + missing = append(missing, keyID) |
| 86 | + } |
| 87 | + if !okSec { |
| 88 | + missing = append(missing, secretAccessKey) |
| 89 | + } |
| 90 | + return nil, status.Errorf( |
| 91 | + codes.InvalidArgument, |
| 92 | + "credentialprovider: missing or invalid keys in Kubernetes Secret: %s", |
| 93 | + strings.Join(missing, ", "), |
| 94 | + ) |
| 95 | +} |
0 commit comments