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

update: authenticate with application credential #86

Merged
Show file tree
Hide file tree
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
67 changes: 38 additions & 29 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import (
)

var (
cfgFile string
email string
password string
region string
project_id string
cfgFile string
email string
password string
region string
project_id string
appCredSecret string
appCredID string
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -66,10 +68,9 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bizfly.yaml)")

rootCmd.PersistentFlags().StringVar(&email, "email", "", "Your Bizfly Cloud Email. Read environment variable BIZFLY_CLOUD_EMAIL")
_ = rootCmd.MarkFlagRequired("email")

rootCmd.PersistentFlags().StringVar(&password, "password", "", "Your Bizfly Cloud Password. Read environment variable BIZFLY_CLOUD_PASSWORD")
_ = rootCmd.MarkFlagRequired("password")
rootCmd.PersistentFlags().StringVar(&appCredID, "app-credential-id", "", "Your Bizfly Cloud Application Credential Id. Read environment variable BIZFLY_CLOUD_APP_CREDENTIAL_ID")
rootCmd.PersistentFlags().StringVar(&appCredSecret, "app-credential-secret", "", "Your Bizfly Cloud Application Credential Secret. Read environment variable BIZFLY_CLOUD_APP_CREDENTIAL_SECRET")

rootCmd.PersistentFlags().StringVar(&region, "region", "HaNoi", "Region you want to access the resource. Read environment variable BIZFLY_CLOUD_REGION")
rootCmd.PersistentFlags().StringVar(&project_id, "project-id", "", "Your Bizfly Cloud Project ID. Read environment variable BIZFLY_CLOUD_PROJECT_ID")
Expand Down Expand Up @@ -107,19 +108,23 @@ func initConfig() {
}

func getApiClient(cmd *cobra.Command) (*gobizfly.Client, context.Context) {

if email == "" {
email = viper.GetString("email")
}
if email == "" {
log.Fatal("Email is required")
// use application credential auth
if appCredID == "" {
appCredID = viper.GetString("app_credential_id")
}

if password == "" {
password = viper.GetString("password")
if appCredSecret == "" {
appCredSecret = viper.GetString("app_credential_secret")
}
if password == "" {
log.Fatal("Password is required")
useAppCredential := true
if appCredID == "" && appCredSecret == "" {
// use username/password auth
if email == "" {
email = viper.GetString("email")
}
if password == "" {
password = viper.GetString("password")
}
useAppCredential = false
}

if viper.GetString("region") != "" {
Expand All @@ -140,20 +145,24 @@ func getApiClient(cmd *cobra.Command) (*gobizfly.Client, context.Context) {
}
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
defer cancelFunc()
// TODO Get token from cache
tok, err := client.Token.Create(ctx,
&gobizfly.TokenCreateRequest{
AuthMethod: "password",
Username: email,
Password: password,
ProjectID: project_id,
})
//TODO Get token from cache
request := &gobizfly.TokenCreateRequest{
ProjectID: project_id,
}
if useAppCredential {
request.AuthType = gobizfly.AppCredentialAuthType
request.AppCredID = appCredID
request.AppCredSecret = appCredSecret
} else {
request.AuthMethod = "password"
request.Username = email
request.Password = password
}
tok, err := client.Token.Create(ctx, request)
if err != nil {
log.Fatal(err)
}

client.SetKeystoneToken(tok)
ctx = context.WithValue(ctx, "token", tok.KeystoneToken)

return client, ctx
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ require (
require github.com/go-openapi/strfmt v0.19.5 // indirect

replace golang.org/x/sys => golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab

replace github.com/bizflycloud/gobizfly v1.0.24 => github.com/duc20176723/gobizfly v0.0.0-20240405103940-5d9abf7389fd
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bizflycloud/gobizfly v1.0.24 h1:iNuu8VCjJ7bfQ7rQja3uaTTr3Ngh/XCkdO0wJLf4ZzY=
github.com/bizflycloud/gobizfly v1.0.24/go.mod h1:Zav85BLm6aSbfM6NRFUmmWzkVfXlQ1YoRTl+dFQJmho=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand All @@ -44,6 +42,8 @@ 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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/duc20176723/gobizfly v0.0.0-20240405103940-5d9abf7389fd h1:G6NnAVGfsqZk2YMwAZXNMLapoy6Rk6CpbihFQ6BErb4=
github.com/duc20176723/gobizfly v0.0.0-20240405103940-5d9abf7389fd/go.mod h1:Zav85BLm6aSbfM6NRFUmmWzkVfXlQ1YoRTl+dFQJmho=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
Loading