-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
529 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// DISCLAIMER | ||
// | ||
// Copyright 2024 ArangoDB GmbH, Cologne, Germany | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
// | ||
|
||
package aws | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/client" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
|
||
"github.com/arangodb/kube-arangodb/pkg/util" | ||
"github.com/arangodb/kube-arangodb/pkg/util/errors" | ||
) | ||
|
||
type Config struct { | ||
Endpoint string | ||
Region string | ||
|
||
DisableSSL bool | ||
|
||
HTTP HTTP | ||
Provider Provider | ||
TLS TLS | ||
} | ||
|
||
func (c Config) GetRegion() string { | ||
if c.Region == "" { | ||
return "us-east-1" | ||
} | ||
|
||
return c.Region | ||
} | ||
|
||
func (c Config) GetProvider() (credentials.Provider, error) { | ||
return c.Provider.Provider() | ||
} | ||
|
||
func (c Config) GetHttpClient() (*http.Client, error) { | ||
tls, err := c.TLS.configuration() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Unable to create TLS") | ||
} | ||
|
||
return &http.Client{ | ||
Transport: c.HTTP.configuration(tls), | ||
}, nil | ||
} | ||
|
||
func (c Config) GetAWSSession() (client.ConfigProvider, error) { | ||
prov, err := c.GetProvider() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Unable to create Provider") | ||
} | ||
|
||
cl, err := c.GetHttpClient() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Unable to create HTTP Client") | ||
} | ||
|
||
return session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{ | ||
Credentials: credentials.NewCredentials(prov), | ||
Endpoint: util.NewType(c.Endpoint), | ||
S3ForcePathStyle: util.NewType(true), | ||
DisableSSL: util.NewType(c.DisableSSL), | ||
HTTPClient: cl, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// DISCLAIMER | ||
// | ||
// Copyright 2024 ArangoDB GmbH, Cologne, Germany | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
// | ||
|
||
package aws | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
|
||
"github.com/arangodb/kube-arangodb/pkg/util" | ||
"github.com/arangodb/kube-arangodb/pkg/util/errors" | ||
) | ||
|
||
type fileProvider struct { | ||
lock sync.Mutex | ||
|
||
accessKeyIDFile string | ||
secretAccessKeyFile string | ||
sessionTokenFile string | ||
|
||
recent time.Time | ||
} | ||
|
||
func (f *fileProvider) Retrieve() (credentials.Value, error) { | ||
if f == nil { | ||
return credentials.Value{}, errors.Errorf("Object is nil") | ||
} | ||
|
||
f.lock.Lock() | ||
defer f.lock.Unlock() | ||
|
||
var v credentials.Value | ||
|
||
v.ProviderName = "dynamic-file-provider" | ||
|
||
if data, err := os.ReadFile(f.accessKeyIDFile); err != nil { | ||
return credentials.Value{}, errors.Wrapf(err, "Unable to open AccessKeyID File") | ||
} else { | ||
v.AccessKeyID = string(data) | ||
} | ||
|
||
if data, err := os.ReadFile(f.secretAccessKeyFile); err != nil { | ||
return credentials.Value{}, errors.Wrapf(err, "Unable to open SecretAccessKey File") | ||
} else { | ||
v.SecretAccessKey = string(data) | ||
} | ||
|
||
if f.sessionTokenFile != "" { | ||
if data, err := os.ReadFile(f.sessionTokenFile); err != nil { | ||
return credentials.Value{}, errors.Wrapf(err, "Unable to open SessionToken File") | ||
} else { | ||
v.SessionToken = string(data) | ||
} | ||
} | ||
|
||
f.recent = util.RecentFileModTime(f.accessKeyIDFile, f.secretAccessKeyFile, f.sessionTokenFile) | ||
|
||
return credentials.Value{}, nil | ||
} | ||
|
||
func (f *fileProvider) IsExpired() bool { | ||
f.lock.Lock() | ||
defer f.lock.Unlock() | ||
|
||
return util.RecentFileModTime(f.accessKeyIDFile, f.secretAccessKeyFile, f.sessionTokenFile).After(f.recent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// DISCLAIMER | ||
// | ||
// Copyright 2024 ArangoDB GmbH, Cologne, Germany | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
// | ||
|
||
package aws | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type HTTP struct { | ||
} | ||
|
||
func (s HTTP) configuration(t *tls.Config) http.RoundTripper { | ||
var c = http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
ForceAttemptHTTP2: true, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
TLSClientConfig: t, | ||
} | ||
|
||
return &c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// DISCLAIMER | ||
// | ||
// Copyright 2024 ArangoDB GmbH, Cologne, Germany | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
// | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/sts" | ||
|
||
"github.com/arangodb/kube-arangodb/pkg/util" | ||
) | ||
|
||
type impersonate struct { | ||
lock sync.Mutex | ||
|
||
credentials credentials.Value | ||
expires time.Time | ||
|
||
config ProviderImpersonate | ||
|
||
creds credentials.Provider | ||
} | ||
|
||
func (i *impersonate) Retrieve() (credentials.Value, error) { | ||
i.lock.Lock() | ||
defer i.lock.Unlock() | ||
|
||
awsSession, err := session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{ | ||
Credentials: credentials.NewCredentials(i.creds), | ||
S3ForcePathStyle: util.NewType(true), | ||
}, | ||
}) | ||
if err != nil { | ||
return credentials.Value{}, err | ||
} | ||
|
||
s := sts.New(awsSession) | ||
|
||
resp, err := s.AssumeRoleWithContext(context.Background(), &sts.AssumeRoleInput{ | ||
RoleArn: util.NewType(i.config.Role), | ||
RoleSessionName: util.NewType(i.config.Name), | ||
}) | ||
if err != nil { | ||
return credentials.Value{}, err | ||
} | ||
|
||
if e := resp.Credentials.Expiration; e != nil { | ||
i.expires = *e | ||
} | ||
|
||
i.credentials = credentials.Value{ | ||
AccessKeyID: util.WithDefault(resp.Credentials.AccessKeyId), | ||
SecretAccessKey: util.WithDefault(resp.Credentials.SecretAccessKey), | ||
SessionToken: util.WithDefault(resp.Credentials.SessionToken), | ||
} | ||
|
||
return i.credentials, nil | ||
} | ||
|
||
func (i *impersonate) IsExpired() bool { | ||
i.lock.Lock() | ||
defer i.lock.Unlock() | ||
|
||
return time.Now().After(i.expires) | ||
} |
Oops, something went wrong.