Skip to content

Commit

Permalink
Use AWS session for obtaining credentials for url signing
Browse files Browse the repository at this point in the history
  • Loading branch information
ManoelMilchev committed Sep 26, 2022
1 parent e59aadb commit 4111b6c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
29 changes: 15 additions & 14 deletions cmd/content-rw-elasticsearch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Financial-Times/content-rw-elasticsearch/v2/pkg/message"
"github.com/Financial-Times/go-logger/v2"
"github.com/Financial-Times/kafka-client-go/v3"
"github.com/aws/aws-sdk-go/aws/session"
cli "github.com/jawher/mow.cli"
)

Expand Down Expand Up @@ -44,16 +45,6 @@ func main() {
Desc: "Logging level (DEBUG, INFO, WARN, ERROR)",
EnvVar: "LOG_LEVEL",
})
accessKey := app.String(cli.StringOpt{
Name: "aws-access-key",
Desc: "AWS ACCESS KEY",
EnvVar: "AWS_ACCESS_KEY_ID",
})
secretKey := app.String(cli.StringOpt{
Name: "aws-secret-access-key",
Desc: "AWS SECRET ACCESS KEY",
EnvVar: "AWS_SECRET_ACCESS_KEY",
})
esEndpoint := app.String(cli.StringOpt{
Name: "elasticsearch-sapi-endpoint",
Value: "http://localhost:9200",
Expand Down Expand Up @@ -109,12 +100,22 @@ func main() {

log := logger.NewUPPLogger(*appSystemCode, *logLevel)
log.Info("[Startup] Application is starting")
awsSession, sessionErr := session.NewSession()
if sessionErr != nil {
log.WithError(sessionErr).Fatal("Failed to initialize AWS session")
}
credValues, err := awsSession.Config.Credentials.Get()
if err != nil {
log.WithError(err).Fatal("Failed to obtain AWS credentials values")
}
log.Infof("Obtaining AWS credentials by using [%s] as provider", credValues.ProviderName)

app.Action = func() {
accessConfig := es.AccessConfig{
AccessKey: *accessKey,
SecretKey: *secretKey,
Endpoint: *esEndpoint,
AccessKey: credValues.AccessKeyID,
SecretKey: credValues.SecretAccessKey,
SessionToken: credValues.SessionToken,
Endpoint: *esEndpoint,
}

httpClient := pkghttp.NewHTTPClient()
Expand Down Expand Up @@ -159,7 +160,7 @@ func main() {

handler.Stop()
}
err := app.Run(os.Args)
err = app.Run(os.Args)
if err != nil {
log.WithError(err).WithTime(time.Now()).Fatal("App could not start")
return
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Financial-Times/kafka-client-go/v3 v3.0.4
github.com/Financial-Times/service-status-go v0.0.0-20160323111542-3f5199736a3d
github.com/Financial-Times/transactionid-utils-go v0.2.0
github.com/aws/aws-sdk-go v1.44.105
github.com/gorilla/mux v1.8.0
github.com/jawher/mow.cli v1.0.4
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
Expand Down Expand Up @@ -35,6 +36,7 @@ require (
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.44.105 h1:UUwoD1PRKIj3ltrDUYTDQj5fOTK3XsnqolLpRTMmSEM=
github.com/aws/aws-sdk-go v1.44.105/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down Expand Up @@ -236,6 +238,10 @@ github.com/jcmturner/gokrb5/v8 v8.4.2 h1:6ZIM6b/JJN0X8UM43ZOM6Z4SJzla+a/u7scXFJz
github.com/jcmturner/gokrb5/v8 v8.4.2/go.mod h1:sb+Xq/fTY5yktf/VxLsE3wlfPqQjp0aWNYyvBVK62bc=
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down Expand Up @@ -286,6 +292,7 @@ github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -446,6 +453,7 @@ golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLd
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down
8 changes: 5 additions & 3 deletions pkg/es/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type Client interface {
}

type AccessConfig struct {
AccessKey string
SecretKey string
Endpoint string
AccessKey string
SecretKey string
SessionToken string
Endpoint string
}

type AWSSigningTransport struct {
Expand All @@ -37,6 +38,7 @@ func NewClient(config AccessConfig, c *http.Client, log *logger.UPPLogger) (Clie
Credentials: awsauth.Credentials{
AccessKeyID: config.AccessKey,
SecretAccessKey: config.SecretKey,
SecurityToken: config.SessionToken,
},
HTTPClient: c,
}
Expand Down

0 comments on commit 4111b6c

Please sign in to comment.