forked from hootsuite/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add IAM credential handling for running in ECS
workaround terraform not using ECS task role for S3 backend hashicorp/terraform-provider-aws#259 hashicorp/terraform-provider-terraform#15
- Loading branch information
Patrick Otto
committed
Nov 17, 2017
1 parent
55c536c
commit df443f8
Showing
3 changed files
with
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package events | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type EcsCredentials struct { | ||
AccessKeyId string | ||
Expiration string | ||
RoleArn string | ||
SecretAccessKey string | ||
Token string | ||
} | ||
|
||
func handleEcsCredentials(relative_uri string) error { | ||
httpClient := &http.Client{Timeout: 5 * time.Second} | ||
url := fmt.Sprintf("http://169.254.170.2%s", relative_uri) | ||
r, err := httpClient.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Body.Close() | ||
|
||
credentials := &EcsCredentials{} | ||
err = json.NewDecoder(r.Body).Decode(credentials) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = writeAwsCredentials(credentials) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func writeAwsCredentials(credentials *EcsCredentials) error { | ||
template := []string{ | ||
"[default]", | ||
fmt.Sprintf("aws_access_key_id=%s", credentials.AccessKeyId), | ||
fmt.Sprintf("aws_secret_access_key=%s", credentials.SecretAccessKey), | ||
fmt.Sprintf("aws_session_token=%s", credentials.Token), | ||
} | ||
|
||
templateRendered := strings.Join(template, "\n") | ||
|
||
err := os.MkdirAll("/home/atlantis/.aws", os.FileMode(0700)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
werr := ioutil.WriteFile("/home/atlantis/.aws/credentials", []byte(templateRendered), 0644) | ||
if werr != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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