Skip to content

Commit

Permalink
Merge pull request #56 from jessesomerville/plugin-support
Browse files Browse the repository at this point in the history
Added plugin support, not complete
  • Loading branch information
jessesomerville authored Apr 19, 2021
2 parents d55d496 + bc6c144 commit 18a12a6
Show file tree
Hide file tree
Showing 43 changed files with 269 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ builds:
- linux
- darwin
ldflags:
- -s -w -X 'github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/appconfig.Version=v{{.Version}}'
- -s -w -X 'github.com/jessesomerville/ephemeral-iam/internal/appconfig.Version=v{{.Version}}'
archives:
- replacements:
darwin: Darwin_macOS
Expand Down
8 changes: 4 additions & 4 deletions cmd/eiam/cmd/assume_privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/proxy"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
"github.com/jessesomerville/ephemeral-iam/internal/proxy"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

var apCmdConfig options.CmdConfig
Expand Down
19 changes: 14 additions & 5 deletions cmd/eiam/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"github.com/lithammer/dedent"
"github.com/spf13/cobra"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
eiam "github.com/jessesomerville/ephemeral-iam/internal"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

var RootCommand *eiam.RootCommand

// NewEphemeralIamCommand returns cobra.Command to run eiam command
func NewEphemeralIamCommand() *cobra.Command {
cmds := &cobra.Command{
func NewEphemeralIamCommand() (*eiam.RootCommand, error) {
cmds := &eiam.RootCommand{Command: cobra.Command{
Use: "eiam",
Short: "Utility for granting short-lived, privileged access to GCP APIs.",
Long: dedent.Dedent(`
Expand Down Expand Up @@ -48,7 +51,7 @@ func NewEphemeralIamCommand() *cobra.Command {
`),
SilenceErrors: true,
SilenceUsage: true,
}
}}

cmds.ResetFlags()

Expand All @@ -57,9 +60,15 @@ func NewEphemeralIamCommand() *cobra.Command {
cmds.AddCommand(newCmdGcloud())
cmds.AddCommand(newCmdKubectl())
cmds.AddCommand(newCmdListServiceAccounts())
cmds.AddCommand(newCmdPlugins())
cmds.AddCommand(newCmdQueryPermissions())
cmds.AddCommand(newCmdVersion())
if err := cmds.LoadPlugins(); err != nil {
return nil, err
}
options.AddPersistentFlags(cmds.PersistentFlags())

return cmds
RootCommand = cmds

return cmds, nil
}
2 changes: 1 addition & 1 deletion cmd/eiam/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/eiam/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

var (
Expand Down
6 changes: 3 additions & 3 deletions cmd/eiam/cmd/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

var (
Expand Down
6 changes: 3 additions & 3 deletions cmd/eiam/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

var (
Expand Down
9 changes: 4 additions & 5 deletions cmd/eiam/cmd/list_service_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/spf13/cobra"
"google.golang.org/api/iam/v1"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

var (
Expand Down Expand Up @@ -83,8 +83,7 @@ func fetchAvailableServiceAccounts() error {

func printColumns(serviceAccounts []*iam.ServiceAccount) {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
fmt.Println()
fmt.Fprintln(w, "EMAIL\tDESCRIPTION")
fmt.Fprintln(w, "\nEMAIL\tDESCRIPTION")
for _, sa := range serviceAccounts {
desc := strings.Split(wordwrap.WrapString(sa.Description, 75), "\n")
if len(desc) == 1 {
Expand Down
51 changes: 51 additions & 0 deletions cmd/eiam/cmd/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"github.com/spf13/cobra"
)

func newCmdPlugins() *cobra.Command {
cmd := &cobra.Command{
Use: "plugins",
Short: "Manage ephemeral-iam plugins",
}

cmd.AddCommand(newCmdPluginsList())
cmd.AddCommand(newCmdPluginsInstall())
cmd.AddCommand(newCmdPluginsRemove())
return cmd
}

func newCmdPluginsList() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Show the list of loaded plugins",
RunE: func(cmd *cobra.Command, args []string) error {
RootCommand.PrintPlugins()
return nil
},
}
return cmd
}

func newCmdPluginsInstall() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "NOT IMPLEMENTED Install a new eiam plugin",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
return cmd
}

func newCmdPluginsRemove() *cobra.Command {
cmd := &cobra.Command{
Use: "remove",
Short: "NOT IMPLEMENTED Remove an installed eiam plugin",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
return cmd
}
10 changes: 5 additions & 5 deletions cmd/eiam/cmd/query_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/lithammer/dedent"
"github.com/spf13/cobra"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd/options"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/appconfig"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
queryiam "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient/query_iam"
"github.com/jessesomerville/ephemeral-iam/internal/appconfig"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
queryiam "github.com/jessesomerville/ephemeral-iam/internal/gcpclient/query_iam"
"github.com/jessesomerville/ephemeral-iam/pkg/options"
)

// Resource string templates
Expand Down
4 changes: 2 additions & 2 deletions cmd/eiam/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/appconfig"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
"github.com/jessesomerville/ephemeral-iam/internal/appconfig"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

func newCmdVersion() *cobra.Command {
Expand Down
5 changes: 3 additions & 2 deletions cmd/eiam/eiam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/cmd"
errorsutil "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/errors"
errorsutil "github.com/jessesomerville/ephemeral-iam/internal/errors"
)

func main() {
cmd := cmd.NewEphemeralIamCommand()
cmd, err := cmd.NewEphemeralIamCommand()
errorsutil.CheckError(err)
errorsutil.CheckError(cmd.Execute())
}
2 changes: 1 addition & 1 deletion docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export PATH="${PATH}:${GOPATH}/bin
#### Install the package
```shell
# Install using go get
$ GO111MODULE="on" go get github.com/jessesomerville/ephemeral-iam/...
$ GO111MODULE="on" go get github.com/jessesomerville/ephemeral-iam
```
This will create the `eiam` binary in your `$GOPATH/bin` directory. You can
Expand Down
1 change: 1 addition & 0 deletions docs/tutorial/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ephemeral-iam Plugins
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/spf13/viper"
"google.golang.org/api/oauth2/v1"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
errorsutil "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/errors"
"github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/gcpclient"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
errorsutil "github.com/jessesomerville/ephemeral-iam/internal/errors"
"github.com/jessesomerville/ephemeral-iam/internal/gcpclient"
)

func init() {
Expand Down Expand Up @@ -145,7 +145,7 @@ func checkADCIdentity(tokenEmail string) error {
util.Logger.Infof("Success. You should now be authenticated as %s", account)
}
} else {
util.Logger.Error("Prompt to select authenticated user failed")
util.Logger.Error("prompt to select authenticated user failed")
}
}
return nil
Expand All @@ -154,14 +154,13 @@ func checkADCIdentity(tokenEmail string) error {
// createLogDir creates the directory to store log files
func createLogDir() error {
logDir := viper.GetString("authproxy.logdir")
_, err := os.Stat(logDir)
if os.IsNotExist(err) {
if _, err := os.Stat(logDir); os.IsNotExist(err) {
util.Logger.Debugf("Creating log directory: %s", logDir)
if err := os.MkdirAll(viper.GetString("authproxy.logdir"), 0o755); err != nil {
return fmt.Errorf("failed to create proxy log directory %s: %v", logDir, err)
}
} else if err != nil {
util.Logger.Errorf("Failed to find proxy log directory: %s", logDir)
util.Logger.Errorf("failed to find proxy log directory: %s", logDir)
return err
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/google/go-github/v33/github"
"github.com/manifoldco/promptui"

archutil "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/appconfig/arch_util"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
archutil "github.com/jessesomerville/ephemeral-iam/internal/appconfig/arch_util"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ func Confirm(vals map[string]string) {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 4, 4, '-', 0)

fmt.Fprintln(w)

for key, val := range vals {
fmt.Fprintf(w, "%s \t %s\n", key, val)
}

w.Flush()
cmdInfo := strings.Split(buf.String(), "\n")

fmt.Println()
for _, line := range cmdInfo {
fmt.Println(line)
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

var invalidCommandErrMsg = regexp.MustCompile(`unknown command "[\S]+" for "[a-z\-]+"`)
Expand Down Expand Up @@ -47,6 +47,10 @@ func CheckError(err error) {
return
}

// ERROR An error occurred error=plugin.Open("/Users/jsomerville/Library/Application Support/ephemeral-iam/plugins/cool_plugin"): plugin was built with a different version of package github.com/jessesomerville/ephemeral-iam/internal/eiamutil function=CheckError line=55
// panic: runtime error: invalid memory address or nil pointer dereference
// [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x48b76cb]

if strings.Contains(err.Error(), "could not find default credentials") {
util.Logger.Fatal("No Application Default Credentials were found. Please run the following command to remediate this issue:\n\n $ gcloud auth application-default login\n\n")
} else if invalidCommandErrMsg.MatchString(err.Error()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package errors

import (
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
"google.golang.org/api/googleapi"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

credentials "cloud.google.com/go/iam/credentials/apiv1"
errorsutil "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/errors"
errorsutil "github.com/jessesomerville/ephemeral-iam/internal/errors"
"google.golang.org/api/option"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/spf13/viper"
"gopkg.in/ini.v1"

util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"

container "cloud.google.com/go/container/apiv1"
util "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/eiamutil"
errorsutil "github.com/jessesomerville/ephemeral-iam/cmd/eiam/internal/errors"
util "github.com/jessesomerville/ephemeral-iam/internal/eiamutil"
errorsutil "github.com/jessesomerville/ephemeral-iam/internal/errors"
"google.golang.org/api/option"
containerpb "google.golang.org/genproto/googleapis/container/v1"
)
Expand Down
Loading

0 comments on commit 18a12a6

Please sign in to comment.