Skip to content

Commit

Permalink
hacked to work with AWS CLI v2 and when using Pyenv.
Browse files Browse the repository at this point in the history
offered to submit to original author to possibly
diagnose some of the weird things that had to be done.
  • Loading branch information
Marakai committed Nov 1, 2022
1 parent 494c571 commit 3ad7926
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
21 changes: 18 additions & 3 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -16,7 +17,6 @@ import (

"github.com/alecthomas/kong"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/skratchdot/open-golang/open"
)

type Context struct {
Expand Down Expand Up @@ -80,12 +80,17 @@ func (cli *Console) consoleURL(signinToken string, destinationURL string) string
return consoleURL.String()
}

// Credentials will only work with one of the latest versions of the AWS Go SDK (see go.mod)
// 1.40.26 was found to work. Versions < 1.40.x would not read profiles that were kept in
// ~/.aws/config but only looked at ~/.aws/credentials
func (cli *Console) Credentials(profile string) (credentials.Value, string, error) {
var sess *session.Session

if profile != "" {
sess = session.Must(session.NewSessionWithOptions(session.Options{
Profile: profile,
// according to some otherwise unrelated Github Issue
SharedConfigState: session.SharedConfigEnable,
}))
} else {
sess = session.Must(session.NewSessionWithOptions(session.Options{
Expand Down Expand Up @@ -140,9 +145,19 @@ func (cli *Console) Run(ctx *Context) error {
return nil
}

err = open.Run(consoleURL)
// Have to run the browser "by hand", because of a discovered issues with AWS CLI v2 env vars
c := exec.Command("xdg-open", consoleURL)
// This is set somewhere in the framework to only the AWS CLI V2 dist path
// to the exclusion of everything else.
os.Unsetenv("LD_LIBRARY_PATH")
c.Env = os.Environ()
out, err := c.Output()
if err != nil {
return fmt.Errorf("error while opening browser, %s", err)
if string(out) == "" {
e := err.(*exec.ExitError)
out = e.Stderr
}
return fmt.Errorf("error while opening browser, %s", out)
}

return nil
Expand Down
22 changes: 19 additions & 3 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
import subprocess
import os
import sys
import site
# Kludge because this will throw up when using AWS CLI v2 for unknown reasons (the file is right there,
# and the path also seems correct)
try:
import site
found_site = True
except:
found_site = False

from awscli.customizations.commands import BasicCommand


Expand Down Expand Up @@ -48,9 +55,18 @@ def _call(self, options, parsed_globals):
"""Run the command."""
cmd = []

bin = os.path.join(site.USER_BASE, 'bin', 'awscli-console-plugin')
if not os.path.isfile(bin):
# miserable hack because there are issue with AWS CLI V2 which causes this plugin to not find site.py
if found_site:
bin = os.path.join(site.USER_BASE, 'bin', 'awscli-console-plugin')
if not os.path.isfile(bin):
bin = os.path.join(sys.prefix, 'bin', 'awscli-console-plugin')
else:
bin = os.path.join(sys.prefix, 'bin', 'awscli-console-plugin')
if not os.path.isfile(bin):
# in addition when using pyenv, the Go executable will be installed somewhere among the shims
# instead of a "normal" location
if os.environ['PYENV_ROOT']:
bin = os.path.join(os.environ['PYENV_ROOT'], 'shims', 'awscli-console-plugin')

cmd.append(bin)

Expand Down
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
module github.com/b-b3rn4rd/awscli-console-plugin

go 1.14
go 1.19

require (
github.com/alecthomas/kong v0.2.11
github.com/aws/aws-sdk-go v1.35.5
github.com/jandelgado/gcov2lcov v1.0.4 // indirect
github.com/aws/aws-sdk-go v1.40.26
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.6.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
20 changes: 8 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
github.com/alecthomas/kong v0.2.11 h1:RKeJXXWfg9N47RYfMm0+igkxBCTF4bzbneAxaqid0c4=
github.com/alecthomas/kong v0.2.11/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/aws/aws-sdk-go v1.35.5 h1:doSEOxC0UkirPcle20Rc+1kAhJ4Ip+GSEeZ3nKl7Qlk=
github.com/aws/aws-sdk-go v1.35.5/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go v1.40.26 h1:th7H+oyDDMQ70CEV4B5kBLULvIV9C5IPg7lef8K3uS4=
github.com/aws/aws-sdk-go v1.40.26/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jandelgado/gcov2lcov v1.0.4 h1:ADwQPyNsxguqzznIbfQTENwY9FU88JdXEvpdHR9c48A=
github.com/jandelgado/gcov2lcov v1.0.4/go.mod h1:NnSxK6TMlg1oGDBfGelGbjgorT5/L3cchlbtgFYZSss=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand All @@ -20,18 +18,16 @@ github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EE
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down

0 comments on commit 3ad7926

Please sign in to comment.