Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 fetching some aws data in the background #4

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var (
version = "dev"
)

const admDuration = 3600
const standardDuration = 36000

Expand All @@ -35,10 +36,10 @@ func main() {
}
iamc := iam.New(cfg)
stsc := sts.New(cfg)
username := username(stsc)
mfaDevice := mfaDevice(iamc, username)
arnChan := mfaDeviceArnChan(stsc, iamc)
tokenCode := tokenCode()
credentials := sessionCredentials(stsc, mfaDevice, tokenCode, duration(profile))
mfaDeviceArn := pullMfaDeviceArn(arnChan)
credentials := sessionCredentials(stsc, mfaDeviceArn, tokenCode, duration(profile))
storeCredentials(profile, region, credentials)
storeConfig(profile, region)
fmt.Printf("Credentials valid until: %s\n", credentials.Expiration)
Expand Down Expand Up @@ -76,26 +77,54 @@ func printVersion() {
fmt.Printf("Skuld version: %s\n", version)
}

func username(stsc *sts.STS) string {
callerIdResp, err := stsc.GetCallerIdentityRequest(nil).Send()
if err != nil {
panic("Unable to get the username.")
}
arn := callerIdResp.Arn
return strings.Split(*arn, ":user/")[1]
type arn struct {
arn string
error interface{}
}

func mfaDeviceArnChan(stsc *sts.STS, iamc *iam.IAM) chan arn {
result := make(chan arn)
go func() {
defer func() {
if err := recover(); err != nil {
result <- arn{error: err}
}
}()
deviceArn := mfaDeviceArn(stsc, iamc)
result <- arn{arn: deviceArn}
}()
return result;
}

func mfaDevice(iamc *iam.IAM, userArn string) string {
func mfaDeviceArn(stsc *sts.STS, iamc *iam.IAM) string {
userArn := userArn(stsc)
mfaDevice, err := iamc.ListMFADevicesRequest(
&iam.ListMFADevicesInput{UserName: &userArn},
).Send()
if err != nil {
println(err.Error())
panic("Unable to fetch the MFA device")
panic("Unable to fetch the MFA device Arn.")
}
return *mfaDevice.MFADevices[0].SerialNumber
}

func userArn(stsc *sts.STS) string {
callerIdResp, err := stsc.GetCallerIdentityRequest(nil).Send()
if err != nil {
panic("Unable to get the userArn.")
}
arn := callerIdResp.Arn
return strings.Split(*arn, ":user/")[1]
}

func pullMfaDeviceArn(arn chan arn) string {
mfaDeviceArn := <-arn
if mfaDeviceArn.error != nil {
panic(mfaDeviceArn.error)
}
return mfaDeviceArn.arn
}

func duration(profile string) int64 {
if strings.HasSuffix(profile, "-adm") {
return admDuration
Expand Down