Skip to content

Commit

Permalink
Merge branch 'master' of github.com:AndreZiviani/aws-fuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Feb 1, 2022
2 parents d3308dc + d854886 commit 4fd0f3c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
26 changes: 25 additions & 1 deletion internal/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ package cache
import (
"database/sql"
"fmt"
"github.com/AndreZiviani/aws-fuzzy/internal/common"
"github.com/faabiosr/cachego"
"github.com/faabiosr/cachego/sqlite3"
_ "github.com/mattn/go-sqlite3"
"os"
)

var (
cacheDir = fmt.Sprintf("%s/.aws-fuzzy/", common.UserHomeDir)
)

// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func New(service string) (cachego.Cache, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("%s/.aws-fuzzy/cache.sqlite", os.Getenv("HOME")))
if ok, err := exists(cacheDir); !ok {
err = os.Mkdir(cacheDir, 0700)
if err != nil {
return nil, err
}
}

db, err := sql.Open("sqlite3", fmt.Sprintf("%s/cache.sqlite", cacheDir))
if err != nil {
fmt.Printf("failed to open cache, %s\n", err)
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
flags "github.com/jessevdk/go-flags"
)

var Parser = flags.NewParser(nil, flags.Default)
var (
Parser = flags.NewParser(nil, flags.Default)
)

func Run() {
sso.Init(Parser)
Expand Down
17 changes: 17 additions & 0 deletions internal/common/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
nmtypes "github.com/aws/aws-sdk-go-v2/service/networkmanager/types"
"os"
)

var (
UserHomeDir, _ = os.UserHomeDir()
)

// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func GetNMTag(tags []nmtypes.Tag, key string, missing string) string {
// Get tag Name
for _, tag := range tags {
Expand Down
13 changes: 9 additions & 4 deletions internal/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"time"

"github.com/AndreZiviani/aws-fuzzy/internal/cache"
"github.com/AndreZiviani/aws-fuzzy/internal/common"
"github.com/AndreZiviani/aws-fuzzy/internal/sso"
"github.com/AndreZiviani/aws-fuzzy/internal/tracing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"os"
"os/exec"
"time"
)

func DoSsh(user, key, ip string) {
Expand Down Expand Up @@ -101,10 +103,13 @@ func (p *SshCommand) Execute(args []string) error {

// Expand ~ if present
if p.Key[0] == '~' {
p.Key = fmt.Sprintf("%s/%s", os.Getenv("HOME"), p.Key[2:])
p.Key = fmt.Sprintf("%s/%s", common.UserHomeDir, p.Key[2:])
}

instances, err := GetInstances(ctx, p.Profile)
if err != nil {
return err
}

span.Finish()

Expand Down
36 changes: 30 additions & 6 deletions internal/sso/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"gopkg.in/ini.v1"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/AndreZiviani/aws-fuzzy/internal/common"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"gopkg.in/ini.v1"
)

var (
configPath = fmt.Sprintf("%s/.aws/config", os.Getenv("HOME"))
configDir = fmt.Sprintf("%s/.aws", common.UserHomeDir)
configPath = fmt.Sprintf("%s/config", configDir)
)

type AwsProfile struct {
Expand Down Expand Up @@ -72,6 +75,18 @@ func (ct *rfc3339) MarshalJSON() ([]byte, error) {
return json.Marshal(ct.Time)
}

// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
Expand Down Expand Up @@ -142,6 +157,11 @@ func WriteSsoProfiles(profiles map[string]AwsProfile) error {
fmt.Printf("could not backup config, %v\n", err)
return err
}
} else {
err = os.Mkdir(configDir, 0700)
if err != nil {
return err
}
}

c := ini.Empty()
Expand Down Expand Up @@ -169,7 +189,11 @@ func WriteSsoProfiles(profiles map[string]AwsProfile) error {
}
func LoadSsoProfiles() (map[string]AwsProfile, error) {
// Load aws config
cfg, _ := ini.Load(configPath)
cfg, err := ini.Load(configPath)

if err != nil {
return nil, err
}

cfg.DeleteSection("DEFAULT")

Expand Down
2 changes: 1 addition & 1 deletion internal/sso/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ConfigureProfiles(ctx context.Context) error {

reader := bufio.NewReader(os.Stdin)

//configPath := fmt.Sprintf("%s/.aws/config", os.Getenv("HOME"))
//configPath := fmt.Sprintf("%s/.aws/config", common.UserHomeDir)

fmt.Print("Enter SSO start url: ")
startUrl, _ := reader.ReadString('\n')
Expand Down
23 changes: 18 additions & 5 deletions internal/sso/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"github.com/AndreZiviani/aws-fuzzy/internal/cache"
"github.com/AndreZiviani/aws-fuzzy/internal/common"
"github.com/AndreZiviani/aws-fuzzy/internal/tracing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
Expand Down Expand Up @@ -57,7 +58,7 @@ func checkExpired(kind string, path string) (interface{}, error) {
}

func checkCachedDevice(cfg aws.Config) (SsoDeviceCredentials, error) {
creds, err := checkExpired("device", fmt.Sprintf("%s/.aws/sso/cache/botocore-client-id-%s.json", os.Getenv("HOME"), cfg.Region))
creds, err := checkExpired("device", fmt.Sprintf("%s/.aws/sso/cache/botocore-client-id-%s.json", common.UserHomeDir, cfg.Region))

if creds == nil {
return SsoDeviceCredentials{}, err
Expand All @@ -78,7 +79,7 @@ func getSessionFileName(startUrl *string) string {
func checkCachedSession(cfg aws.Config, startUrl *string) (SsoSessionCredentials, error) {
hash := getSessionFileName(startUrl)

creds, err := checkExpired("session", fmt.Sprintf("%s/.aws/sso/cache/%x.json", os.Getenv("HOME"), hash))
creds, err := checkExpired("session", fmt.Sprintf("%s/.aws/sso/cache/%x.json", common.UserHomeDir, hash))

if creds == nil {
return SsoSessionCredentials{}, err
Expand All @@ -88,13 +89,21 @@ func checkCachedSession(cfg aws.Config, startUrl *string) (SsoSessionCredentials
}

func cacheCredentials(device *SsoDeviceCredentials, session *SsoSessionCredentials) error {
ssoCacheDir := fmt.Sprintf("%s/.aws/sso/cache/", common.UserHomeDir)
if ok, err := exists(ssoCacheDir); !ok {
err = os.MkdirAll(ssoCacheDir, 0700)
if err != nil {
return err
}
}

hash := getSessionFileName(session.StartUrl)

file, _ := json.Marshal(session)
_ = ioutil.WriteFile(fmt.Sprintf("%s/.aws/sso/cache/%x.json", os.Getenv("HOME"), hash), file, 0600)
_ = ioutil.WriteFile(fmt.Sprintf("%s/.aws/sso/cache/%x.json", common.UserHomeDir, hash), file, 0600)

file, _ = json.Marshal(device)
_ = ioutil.WriteFile(fmt.Sprintf("%s/.aws/sso/cache/botocore-client-id-%s.json", os.Getenv("HOME"), session.Region), file, 0600)
_ = ioutil.WriteFile(fmt.Sprintf("%s/.aws/sso/cache/botocore-client-id-%s.json", common.UserHomeDir, session.Region), file, 0600)
return nil

}
Expand Down Expand Up @@ -268,7 +277,7 @@ func GetCredentials(ctx context.Context, profile string, ask bool) (*aws.Credent

creds, err = provider.Retrieve(ctx)
if err != nil {
fmt.Fprintf(os.stderr, "failed to get role credentials, %s\n", err)
fmt.Fprintf(os.Stderr, "failed to get role credentials, %s\n", err)
SsoLogin(ctx)
// if we get here we have an expired sso token and user realy wants to login, dont need to ask again
return GetCredentials(ctx, profile, false)
Expand Down Expand Up @@ -311,6 +320,10 @@ func (p *LoginCommand) Execute(args []string) error {
defer spanSso.Finish()

creds, err := GetCredentials(ctx, p.Profile, p.Ask)
if err != nil {
return err
}

PrintCredentials(creds)

return err
Expand Down

0 comments on commit 4fd0f3c

Please sign in to comment.