Skip to content

Commit b7797bc

Browse files
author
Will Chan
committed
added tests and refactored constants to match golang naming conventions
1 parent a3be1d7 commit b7797bc

File tree

7 files changed

+79
-66
lines changed

7 files changed

+79
-66
lines changed

auth.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
)
1313

1414
const (
15-
ACCESS_ENV_KEY = "AWS_ACCESS_KEY"
16-
SECRET_ENV_KEY = "AWS_SECRET_KEY"
15+
AccessEnvKey = "AWS_ACCESS_KEY"
16+
SecretEnvKey = "AWS_SECRET_KEY"
1717

18-
AWS_METADATA_SERVER = "169.254.169.254"
19-
AWS_IAM_CREDS_PATH = "/latest/meta-data/iam/security-credentials"
20-
AWS_IAM_CREDS_URL = "http://" + AWS_METADATA_SERVER + AWS_IAM_CREDS_PATH
18+
AWSMetadataServer = "169.254.169.254"
19+
AWSIAMCredsPath = "/latest/meta-data/iam/security-credentials"
20+
AWSIAMCredsURL = "http://" + AWSMetadataServer + "/" + AWSIAMCredsPath
2121
)
2222

2323
// Auth interface for authentication credentials and information
@@ -31,7 +31,8 @@ type Auth interface {
3131
Sign(*Service, time.Time) []byte
3232
}
3333

34-
type auth struct {
34+
// AuthCredentials holds the AWS credentials and metadata
35+
type AuthCredentials struct {
3536
// accessKey, secretKey are the standard AWS auth credentials
3637
accessKey, secretKey, token string
3738

@@ -41,22 +42,24 @@ type auth struct {
4142
expiry time.Time
4243
}
4344

44-
func NewAuth(accessKey, secretKey string) Auth {
45-
return &auth{
45+
// NewAuth creates a *AuthCredentials struct that adheres to the Auth interface to
46+
// dynamically retrieve AWS credentials
47+
func NewAuth(accessKey, secretKey string) *AuthCredentials {
48+
return &AuthCredentials{
4649
accessKey: accessKey,
4750
secretKey: secretKey,
4851
}
4952
}
5053

5154
// NewAuthFromEnv retrieves auth credentials from environment vars
52-
func NewAuthFromEnv() (Auth, error) {
53-
accessKey := os.Getenv(ACCESS_ENV_KEY)
54-
secretKey := os.Getenv(SECRET_ENV_KEY)
55+
func NewAuthFromEnv() (*AuthCredentials, error) {
56+
accessKey := os.Getenv(AccessEnvKey)
57+
secretKey := os.Getenv(SecretEnvKey)
5558
if accessKey == "" {
56-
return nil, fmt.Errorf("Unable to retrieve access key from %s env variable", ACCESS_ENV_KEY)
59+
return nil, fmt.Errorf("Unable to retrieve access key from %s env variable", AccessEnvKey)
5760
}
5861
if secretKey == "" {
59-
return nil, fmt.Errorf("Unable to retrieve secret key from %s env variable", SECRET_ENV_KEY)
62+
return nil, fmt.Errorf("Unable to retrieve secret key from %s env variable", SecretEnvKey)
6063
}
6164

6265
return NewAuth(accessKey, secretKey), nil
@@ -68,41 +71,41 @@ func NewAuthFromEnv() (Auth, error) {
6871
//
6972
// TODO: specify custom network (connect, read) timeouts, else this will block
7073
// for the default timeout durations.
71-
func NewAuthFromMetadata() (Auth, error) {
72-
auth := &auth{}
74+
func NewAuthFromMetadata() (*AuthCredentials, error) {
75+
auth := &AuthCredentials{}
7376
if err := auth.Renew(); err != nil {
7477
return nil, err
7578
}
7679
return auth, nil
7780
}
7881

7982
// HasExpiration returns true if the expiration time is non-zero and false otherwise
80-
func (a *auth) HasExpiration() bool {
83+
func (a *AuthCredentials) HasExpiration() bool {
8184
return !a.expiry.IsZero()
8285
}
8386

8487
// GetExpiration retrieves the current expiration time
85-
func (a *auth) GetExpiration() time.Time {
88+
func (a *AuthCredentials) GetExpiration() time.Time {
8689
return a.expiry
8790
}
8891

8992
// GetToken returns the token
90-
func (a *auth) GetToken() string {
93+
func (a *AuthCredentials) GetToken() string {
9194
return a.token
9295
}
9396

9497
// GetSecretKey returns the secret key
95-
func (a *auth) GetSecretKey() string {
98+
func (a *AuthCredentials) GetSecretKey() string {
9699
return a.secretKey
97100
}
98101

99102
// GetAccessKey returns the access key
100-
func (a *auth) GetAccessKey() string {
103+
func (a *AuthCredentials) GetAccessKey() string {
101104
return a.accessKey
102105
}
103106

104107
// Renew retrieves a new token and mutates it on an instance of the Auth struct
105-
func (a *auth) Renew() error {
108+
func (a *AuthCredentials) Renew() error {
106109
role, err := retrieveIAMRole()
107110
if err != nil {
108111
return err
@@ -127,7 +130,7 @@ func (a *auth) Renew() error {
127130
// Sign API request by
128131
// http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html
129132

130-
func (a *auth) Sign(s *Service, t time.Time) []byte {
133+
func (a *AuthCredentials) Sign(s *Service, t time.Time) []byte {
131134
h := ghmac([]byte("AWS4"+a.GetSecretKey()), []byte(t.Format(iSO8601BasicFormatShort)))
132135
h = ghmac(h, []byte(s.Region))
133136
h = ghmac(h, []byte(s.Name))
@@ -138,7 +141,7 @@ func (a *auth) Sign(s *Service, t time.Time) []byte {
138141
func retrieveAWSCredentials(role string) (map[string]string, error) {
139142
var bodybytes []byte
140143
// Retrieve the json for this role
141-
resp, err := http.Get(AWS_IAM_CREDS_URL + "/" + role)
144+
resp, err := http.Get(fmt.Sprintf("%s/%s", AWSIAMCredsURL, role))
142145
if err != nil || resp.StatusCode != http.StatusOK {
143146
return nil, err
144147
}
@@ -161,7 +164,7 @@ func retrieveAWSCredentials(role string) (map[string]string, error) {
161164
func retrieveIAMRole() (string, error) {
162165
var bodybytes []byte
163166

164-
resp, err := http.Get(AWS_IAM_CREDS_URL)
167+
resp, err := http.Get(AWSIAMCredsURL)
165168
if err != nil || resp.StatusCode != http.StatusOK {
166169
return "", err
167170
}

auth_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import (
55
"testing"
66
)
77

8+
func TestAuthInterfaceIsImplemented(t *testing.T) {
9+
var auth Auth = &AuthCredentials{}
10+
if auth == nil {
11+
t.Error("Invalid nil auth credentials value")
12+
}
13+
}
14+
815
func TestGetSecretKey(t *testing.T) {
916
auth := NewAuth("BAD_ACCESS_KEY", "BAD_SECRET_KEY")
1017

@@ -22,8 +29,8 @@ func TestGetAccessKey(t *testing.T) {
2229
}
2330

2431
func TestNewAuthFromEnv(t *testing.T) {
25-
os.Setenv(ACCESS_ENV_KEY, "asdf")
26-
os.Setenv(SECRET_ENV_KEY, "asdf")
32+
os.Setenv(AccessEnvKey, "asdf")
33+
os.Setenv(SecretEnvKey, "asdf")
2734

2835
auth, _ := NewAuthFromEnv()
2936

@@ -35,6 +42,6 @@ func TestNewAuthFromEnv(t *testing.T) {
3542
t.Error("Expected SecretKey to be inferred as \"asdf\"")
3643
}
3744

38-
os.Setenv(ACCESS_ENV_KEY, "") // Use Unsetenv with go1.4
39-
os.Setenv(SECRET_ENV_KEY, "") // Use Unsetenv with go1.4
45+
os.Setenv(AccessEnvKey, "") // Use Unsetenv with go1.4
46+
os.Setenv(SecretEnvKey, "") // Use Unsetenv with go1.4
4047
}

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66
)
77

8-
const AWS_SECURITY_TOKEN_HEADER = "X-Amz-Security-Token"
8+
const AWSSecurityTokenHeader = "X-Amz-Security-Token"
99

1010
// Client is like http.Client, but signs all requests using Auth.
1111
type Client struct {
@@ -24,6 +24,10 @@ func NewClient(auth Auth) *Client {
2424
return &Client{auth: auth, client: http.DefaultClient}
2525
}
2626

27+
// NewClientWithHTTPClient creates a client with a non-default http client
28+
// ie. a timeout could be set on the HTTP client to timeout if Kinesis doesn't
29+
// response in a timely manner like after the 5 minute mark where the current
30+
// shard iterator expires
2731
func NewClientWithHTTPClient(auth Auth, httpClient *http.Client) *Client {
2832
return &Client{auth: auth, client: httpClient}
2933
}
@@ -42,7 +46,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
4246
}
4347

4448
if c.auth.GetToken() != "" {
45-
req.Header.Add(AWS_SECURITY_TOKEN_HEADER, c.auth.GetToken())
49+
req.Header.Add(AWSSecurityTokenHeader, c.auth.GetToken())
4650
}
4751

4852
return c.client.Do(req)

examples/example.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"time"
77

8+
// kinesis "github.com/sendgridlabs/go-kinesis"
89
kinesis "github.com/sendgridlabs/go-kinesis"
910
)
1011

kinesis-cli/kinesis-cli.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strconv"
2323
"strings"
2424

25+
// "github.com/sendgridlabs/go-kinesis"
2526
"github.com/sendgridlabs/go-kinesis"
2627
)
2728

@@ -117,14 +118,14 @@ func main() {
117118
if len(os.Args) < 2 {
118119
die(true, "Error: no command specified.")
119120
}
120-
if os.Getenv(kinesis.ACCESS_ENV_KEY) == "" ||
121-
os.Getenv(kinesis.SECRET_ENV_KEY) == "" {
121+
if os.Getenv(kinesis.AccessEnvKey) == "" ||
122+
os.Getenv(kinesis.SecretEnvKey) == "" {
122123
fmt.Printf("WARNING: %s and/or %s environment variables not set. Will "+
123124
"attempt to fetch credentials from metadata server.\n",
124-
kinesis.ACCESS_ENV_KEY, kinesis.SECRET_ENV_KEY)
125+
kinesis.AccessEnvKey, kinesis.SecretEnvKey)
125126
}
126-
if os.Getenv(kinesis.REGION_ENV_NAME) == "" {
127-
fmt.Printf("WARNING: %s not set.\n", kinesis.REGION_ENV_NAME)
127+
if os.Getenv(kinesis.RegionEnvName) == "" {
128+
fmt.Printf("WARNING: %s not set.\n", kinesis.RegionEnvName)
128129
}
129130
switch os.Args[1] {
130131
case "create":

0 commit comments

Comments
 (0)