Skip to content
This repository was archived by the owner on Feb 7, 2020. It is now read-only.

Commit 60438a1

Browse files
authored
Merge pull request #10 from rande/fix_aws_metadata
fix(aws): make sure aws metadata session is correctly set
2 parents 3df1bb3 + a6890f3 commit 60438a1

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
language: go
22

3-
go: [1.6]
3+
go: [1.9]
44

55
sudo: required
66

77
services :
88
- docker
99

1010
before_install:
11-
- docker pull golang:1.6-alpine
11+
- docker pull golang:1.9-alpine
1212

1313
script:
1414
- go get github.com/mattn/goveralls

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ build: ## build binaries
4040
GOOS=linux GOARCH=arm go build -ldflags "-X main.RefLog=$(SHA1) -s -w" -o build/linux-arm-gitlab-ci-helper cli/main.go
4141
GOOS=linux GOARCH=arm64 go build -ldflags "-X main.RefLog=$(SHA1) -s -w" -o build/linux-arm64-gitlab-ci-helper cli/main.go
4242
ifneq ("", "$(shell which docker)")
43-
docker run --rm -v $(shell pwd):/usr/src/myapp -v $(GOPATH):/usr/src/myapp/vendor -w /usr/src/myapp -e "GOPATH=/usr/src/myapp/vendor:/go" -e GOOS=linux -e GOARCH=amd64 golang:1.6-alpine go build -ldflags "-X main.RefLog=$(SHA1) -s -w" -o build/alpine-amd64-gitlab-ci-helper cli/main.go
43+
docker run --rm -v $(shell pwd):/usr/src/myapp -v $(GOPATH):/usr/src/myapp/vendor -w /usr/src/myapp -e "GOPATH=/usr/src/myapp/vendor:/go" -e GOOS=linux -e GOARCH=amd64 golang:1.9-alpine go build -ldflags "-X main.RefLog=$(SHA1) -s -w" -o build/alpine-amd64-gitlab-ci-helper cli/main.go
4444
endif
4545

4646
coverage-backend: ## run coverage tests

commands/s3_archive.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ package commands
88
import (
99
"flag"
1010
"fmt"
11-
"github.com/mitchellh/cli"
12-
gitlab "github.com/plouc/go-gitlab-client"
13-
helper "github.com/rande/gitlab-ci-helper"
1411
"os"
12+
"regexp"
1513
"strings"
14+
"time"
1615

1716
"github.com/aws/aws-sdk-go/aws"
18-
"github.com/aws/aws-sdk-go/aws/credentials"
19-
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2017
"github.com/aws/aws-sdk-go/aws/session"
2118
"github.com/aws/aws-sdk-go/service/s3"
22-
"regexp"
23-
"time"
19+
"github.com/mitchellh/cli"
20+
gitlab "github.com/plouc/go-gitlab-client"
21+
helper "github.com/rande/gitlab-ci-helper"
2422
)
2523

2624
type S3ArchiveCommand struct {
@@ -105,16 +103,7 @@ func (c *S3ArchiveCommand) Run(args []string) int {
105103
return 1
106104
}
107105

108-
chainProvider := credentials.NewChainCredentials([]credentials.Provider{
109-
&credentials.EnvProvider{},
110-
&credentials.SharedCredentialsProvider{
111-
Filename: os.Getenv("HOME") + "/.aws/credentials",
112-
Profile: c.AwsProfile,
113-
},
114-
&ec2rolecreds.EC2RoleProvider{},
115-
})
116-
117-
_, err = chainProvider.Get()
106+
credentials, err := helper.GetAwsCredentials(c.AwsProfile)
118107

119108
if err != nil {
120109
c.Ui.Output(fmt.Sprintf("Unable to load credentials: %s", err))
@@ -126,7 +115,7 @@ func (c *S3ArchiveCommand) Run(args []string) int {
126115
Region: aws.String(c.AwsRegion),
127116
Endpoint: aws.String(c.AwsEndPoint),
128117
S3ForcePathStyle: aws.Bool(true),
129-
Credentials: chainProvider,
118+
Credentials: credentials,
130119
}
131120

132121
s3client := s3.New(session.New(), awsConfig)

commands/s3_extract.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ package commands
88
import (
99
"flag"
1010
"fmt"
11+
"os"
12+
"strings"
13+
1114
"github.com/mitchellh/cli"
1215
gitlab "github.com/plouc/go-gitlab-client"
1316
helper "github.com/rande/gitlab-ci-helper"
14-
"os"
15-
"strings"
17+
18+
"io"
19+
"regexp"
1620

1721
"github.com/aws/aws-sdk-go/aws"
18-
"github.com/aws/aws-sdk-go/aws/credentials"
19-
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2022
"github.com/aws/aws-sdk-go/aws/session"
2123
"github.com/aws/aws-sdk-go/service/s3"
22-
"io"
23-
"regexp"
2424
)
2525

2626
type S3ExtractCommand struct {
@@ -76,16 +76,7 @@ func (c *S3ExtractCommand) Run(args []string) int {
7676
return 1
7777
}
7878

79-
chainProvider := credentials.NewChainCredentials([]credentials.Provider{
80-
&credentials.EnvProvider{},
81-
&credentials.SharedCredentialsProvider{
82-
Filename: os.Getenv("HOME") + "/.aws/credentials",
83-
Profile: c.AwsProfile,
84-
},
85-
&ec2rolecreds.EC2RoleProvider{},
86-
})
87-
88-
_, err = chainProvider.Get()
79+
credentials, err := helper.GetAwsCredentials(c.Project)
8980

9081
if err != nil {
9182
c.Ui.Output(fmt.Sprintf("Unable to load credentials: %s", err))
@@ -97,7 +88,7 @@ func (c *S3ExtractCommand) Run(args []string) int {
9788
Region: aws.String(c.AwsRegion),
9889
Endpoint: aws.String(c.AwsEndPoint),
9990
S3ForcePathStyle: aws.Bool(true),
100-
Credentials: chainProvider,
91+
Credentials: credentials,
10192
}
10293

10394
s3client := s3.New(session.New(), awsConfig)

helper.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"bytes"
1010
"errors"
1111
"fmt"
12-
gitlab "github.com/plouc/go-gitlab-client"
1312
"io"
1413
"net/http"
1514
"net/http/httptest"
@@ -18,6 +17,14 @@ import (
1817
"strconv"
1918
"strings"
2019
"testing"
20+
"time"
21+
22+
"github.com/aws/aws-sdk-go/aws"
23+
"github.com/aws/aws-sdk-go/aws/credentials"
24+
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
25+
"github.com/aws/aws-sdk-go/aws/ec2metadata"
26+
"github.com/aws/aws-sdk-go/aws/session"
27+
gitlab "github.com/plouc/go-gitlab-client"
2128
)
2229

2330
type Paths []string
@@ -34,6 +41,32 @@ func (p *Paths) Set(value string) error {
3441

3542
var SemVersion = regexp.MustCompile("(v|)[0-9]{1,}\\.[0-9]{1,}\\.[0-9]{1,}(-[A-Za-z]*|)")
3643

44+
func GetAwsCredentials(profile string) (*credentials.Credentials, error) {
45+
sess := session.Must(session.NewSession())
46+
47+
chainProvider := credentials.NewChainCredentials([]credentials.Provider{
48+
&credentials.EnvProvider{},
49+
&credentials.SharedCredentialsProvider{
50+
Filename: os.Getenv("HOME") + "/.aws/credentials",
51+
Profile: profile,
52+
},
53+
&ec2rolecreds.EC2RoleProvider{
54+
Client: ec2metadata.New(sess, &aws.Config{
55+
HTTPClient: &http.Client{Timeout: 10 * time.Second},
56+
}),
57+
ExpiryWindow: 0,
58+
},
59+
})
60+
61+
_, err := chainProvider.Get()
62+
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return chainProvider, nil
68+
}
69+
3770
func GetProject(p string, client *gitlab.Gitlab) (*gitlab.Project, error) {
3871
pId, err := strconv.ParseInt(p, 10, 32)
3972

@@ -42,7 +75,7 @@ func GetProject(p string, client *gitlab.Gitlab) (*gitlab.Project, error) {
4275
paths := strings.Split(p, "/")
4376

4477
if len(paths) != 2 {
45-
return nil, errors.New("Error: Invalid project format, must be namespace/project-name")
78+
return nil, fmt.Errorf("Error: Invalid project format, must be namespace/project-name, project=%s, err=%s", p, err)
4679
}
4780

4881
projects, _ := client.Projects()

integrations/hipchat/ci_message_hipchat.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ func (c *CiNotificationHipchatCommand) Run(args []string) int {
5454

5555
config := &HipChatConfig{}
5656
message := &HipChatMessage{
57-
//Card: &HipChatCard{
58-
// Description: &HipChatDescription {
59-
// Value: 1,
60-
// Format: "html",
61-
// },
62-
//},
57+
//Card: &HipChatCard{
58+
// Description: &HipChatDescription {
59+
// Value: 1,
60+
// Format: "html",
61+
// },
62+
//},
6363
}
6464

6565
cmdFlags := flag.NewFlagSet("ci:notification:hipchat", flag.ContinueOnError)

0 commit comments

Comments
 (0)