|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package aws |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/aws/aws-sdk-go/aws" |
| 29 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 30 | + "github.com/aws/aws-sdk-go/aws/session" |
| 31 | + "github.com/aws/aws-sdk-go/service/sts" |
| 32 | + |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 34 | +) |
| 35 | + |
| 36 | +type impersonate struct { |
| 37 | + lock sync.Mutex |
| 38 | + |
| 39 | + credentials credentials.Value |
| 40 | + expires time.Time |
| 41 | + |
| 42 | + config ProviderImpersonate |
| 43 | + |
| 44 | + creds credentials.Provider |
| 45 | +} |
| 46 | + |
| 47 | +func (i *impersonate) Retrieve() (credentials.Value, error) { |
| 48 | + i.lock.Lock() |
| 49 | + defer i.lock.Unlock() |
| 50 | + |
| 51 | + awsSession, err := session.NewSessionWithOptions(session.Options{ |
| 52 | + Config: aws.Config{ |
| 53 | + Credentials: credentials.NewCredentials(i.creds), |
| 54 | + S3ForcePathStyle: util.NewType(true), |
| 55 | + }, |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + return credentials.Value{}, err |
| 59 | + } |
| 60 | + |
| 61 | + s := sts.New(awsSession) |
| 62 | + |
| 63 | + resp, err := s.AssumeRoleWithContext(context.Background(), &sts.AssumeRoleInput{ |
| 64 | + RoleArn: util.NewType(i.config.Role), |
| 65 | + RoleSessionName: util.NewType(i.config.Name), |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return credentials.Value{}, err |
| 69 | + } |
| 70 | + |
| 71 | + if e := resp.Credentials.Expiration; e != nil { |
| 72 | + i.expires = *e |
| 73 | + } |
| 74 | + |
| 75 | + i.credentials = credentials.Value{ |
| 76 | + AccessKeyID: util.WithDefault(resp.Credentials.AccessKeyId), |
| 77 | + SecretAccessKey: util.WithDefault(resp.Credentials.SecretAccessKey), |
| 78 | + SessionToken: util.WithDefault(resp.Credentials.SessionToken), |
| 79 | + } |
| 80 | + |
| 81 | + return i.credentials, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (i *impersonate) IsExpired() bool { |
| 85 | + i.lock.Lock() |
| 86 | + defer i.lock.Unlock() |
| 87 | + |
| 88 | + return time.Now().After(i.expires) |
| 89 | +} |
0 commit comments