Skip to content

Commit

Permalink
feat: adds support for GDAC #317
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Oct 17, 2023
1 parent d8b1e90 commit e62a924
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
27 changes: 20 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ var RootCmd = &cobra.Command{
Short: "Utility to work with Apigee APIs.",
Long: "This command lets you interact with Apigee APIs.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if metadataToken && defaultToken {
return fmt.Errorf("metadata-token and default-token cannot be used together")
}
if defaultToken && (serviceAccount != "" || accessToken != "") {
return fmt.Errorf("default-token cannot be used with token or account flags")
}
if metadataToken && (serviceAccount != "" || accessToken != "") {
return fmt.Errorf("metadata-token cannot be used with token or account flags")
}
Expand All @@ -71,11 +77,6 @@ var RootCmd = &cobra.Command{
return fmt.Errorf("token and account flags cannot be used together")
}

if !metadataToken {
apiclient.SetServiceAccount(serviceAccount)
apiclient.SetApigeeToken(accessToken)
}

if !disableCheck {
if ok, _ := apiclient.TestAndUpdateLastCheck(); !ok {
latestVersion, _ := getLatestVersion()
Expand All @@ -88,7 +89,16 @@ var RootCmd = &cobra.Command{
}
}

if !metadataToken && !defaultToken {
apiclient.SetServiceAccount(serviceAccount)
apiclient.SetApigeeToken(accessToken)
}

if metadataToken {
return apiclient.GetMetadataAccessToken()
}

if defaultToken {
return apiclient.GetDefaultAccessToken()
}

Expand All @@ -107,8 +117,8 @@ func Execute() {
}

var (
accessToken, serviceAccount string
disableCheck, printOutput, noOutput, metadataToken bool
accessToken, serviceAccount string
disableCheck, printOutput, noOutput, metadataToken, defaultToken bool
)

const ENABLED = "true"
Expand All @@ -134,6 +144,9 @@ func init() {
RootCmd.PersistentFlags().BoolVarP(&metadataToken, "metadata-token", "",
false, "Metadata OAuth2 access token")

RootCmd.PersistentFlags().BoolVarP(&defaultToken, "default-token", "",
false, "Use Google defalt application credentials access token")

RootCmd.AddCommand(apis.Cmd)
RootCmd.AddCommand(org.Cmd)
RootCmd.AddCommand(sync.Cmd)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
)

require (
cloud.google.com/go/compute/metadata v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/getkin/kin-openapi v0.115.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cloud.google.com/go/compute/metadata v0.2.0 h1:nBbNSZyDpkNlo3DepaaLKVuO7ClyifSAmNloSCZrHnQ=
cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
17 changes: 17 additions & 0 deletions internal/apiclient/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package apiclient

import (
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
Expand All @@ -33,6 +34,7 @@ import (

"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
"golang.org/x/oauth2/google"
)

type serviceAccount struct {
Expand Down Expand Up @@ -313,6 +315,21 @@ func getMetadata(metadata string) (respBpdy []byte, err error) {

// GetDefaultAccessToken
func GetDefaultAccessToken() (err error) {
ctx := context.Background()
tokenSource, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return err
}
token, err := tokenSource.Token()
if err != nil {
return err
}
SetApigeeToken(token.AccessToken)
return nil
}

// GetMetadataAccessToken
func GetMetadataAccessToken() (err error) {
var tokenResponse map[string]interface{}

respBody, err := getMetadata("token")
Expand Down

0 comments on commit e62a924

Please sign in to comment.