Skip to content

Commit

Permalink
Merge pull request #3 from neo4j/use-default-credentials
Browse files Browse the repository at this point in the history
Use default credentials in config for requests
  • Loading branch information
angrykoala authored Jul 31, 2024
2 parents e84e23f + ba89092 commit c30a35d
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 21 deletions.
30 changes: 19 additions & 11 deletions pkg/aura/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ import (

const userAgent = "Neo4jCLI/%s"

type Grant struct {
AccessToken string `json:"access_token"`
//ExpiresIn string `json:"expires_in"`
}

func getToken(ctx context.Context) (string, error) {
type Grant struct {
AccessToken string `json:"access_token"`
// expiresIn string `json:"expires_in"`
config, ok := clictx.Config(ctx)
if !ok {
return "", errors.New("error fetching cli configuration values")
}

client := http.Client{}
credential, err := config.Aura.GetDefaultCredential()
if err != nil {
return "", err
}

data := url.Values{}
data.Set("grant_type", "client_credentials")
// If token exists and not expire rate

config, ok := clictx.Config(ctx)
data := url.Values{}

if !ok {
return "", errors.New("error fetching cli configuration values")
}
data.Set("grant_type", "client_credentials")

u, err := config.Get("aura.auth-url")

Expand All @@ -55,8 +60,9 @@ func getToken(ctx context.Context) (string, error) {
"Content-Type": {"application/x-www-form-urlencoded"},
"User-Agent": {fmt.Sprintf(userAgent, version)},
}
req.SetBasicAuth(credential.ClientId, credential.ClientSecret)

req.SetBasicAuth("9nNx3JS9gfTGAVaD7RnVfViWpimYkNwN", "gV-_AibheBDqZnNI7sePG807m8jbEq_ICqIwPYGLRwWBwqnQb7KJ39A0GUkCSbgD")
client := http.Client{}

res, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -90,6 +96,8 @@ func getToken(ctx context.Context) (string, error) {
return "", err
}

// Save Grant in config

return grant.AccessToken, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/aura/cmd/credential/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAddCredential(t *testing.T) {
out, err := io.ReadAll(b)
assert.Nil(err)

assert.Equal(fmt.Sprintf(`{"aura":{"base-url":"%s","auth-url":"%s","output":"json","credentials":[{"name":"test","client-id":"testclientid","client-secret":"testclientsecret"}]}}`, clicfg.DefaultAuraBaseUrl, clicfg.DefaultAuraAuthUrl), string(out))
assert.Equal(fmt.Sprintf(`{"aura":{"base-url":"%s","auth-url":"%s","output":"json","credentials":[{"name":"test","client-id":"testclientid","client-secret":"testclientsecret","access-token":"","token-expiry":""}]}}`, clicfg.DefaultAuraBaseUrl, clicfg.DefaultAuraAuthUrl), string(out))
}

func TestAddCredentialIfAlreadyExists(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/aura/cmd/credential/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestUseCredential(t *testing.T) {
out, err := io.ReadAll(b)
assert.Nil(err)

assert.Equal(fmt.Sprintf(`{"aura":{"base-url":"%s","auth-url":"%s","output":"json","default-credential":"test","credentials":[{"name":"test","client-id":"testclientid","client-secret":"testclientsecret"}]}}`, clicfg.DefaultAuraBaseUrl, clicfg.DefaultAuraAuthUrl), string(out))
assert.Equal(fmt.Sprintf(`{"aura":{"base-url":"%s","auth-url":"%s","output":"json","default-credential":"test","credentials":[{"name":"test","client-id":"testclientid","client-secret":"testclientsecret","access-token":"","token-expiry":""}]}}`, clicfg.DefaultAuraBaseUrl, clicfg.DefaultAuraAuthUrl), string(out))
}

// TODO: currently fails when running with all tests - figure out what is going here
Expand Down
22 changes: 20 additions & 2 deletions pkg/aura/cmd/customermanagedkey/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ func TestGetCustomerManagedKey(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"customer-managed-key", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), cmkId})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down Expand Up @@ -139,7 +148,16 @@ func TestGetCustomerManagedKeyAlias(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"cmk", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), cmkId})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
22 changes: 20 additions & 2 deletions pkg/aura/cmd/customermanagedkey/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ func TestListCustomerManagedKeys(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"customer-managed-key", "list", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down Expand Up @@ -138,7 +147,16 @@ func TestListCustomerManagedKeysAlias(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"cmk", "list", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
11 changes: 10 additions & 1 deletion pkg/aura/cmd/instance/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ func TestGetInstance(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"instance", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), instanceId})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
11 changes: 10 additions & 1 deletion pkg/aura/cmd/instance/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ func TestListInstances(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"instance", "list", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
11 changes: 10 additions & 1 deletion pkg/aura/cmd/tenant/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ func TestGetTenant(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"tenant", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), tenantId})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
11 changes: 10 additions & 1 deletion pkg/aura/cmd/tenant/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ func TestListTenants(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"tenant", "list", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)})

cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{}`), nil)
cfg, err := clicfg.NewConfigFrom(strings.NewReader(`{
"aura": {
"credentials": [{
"name": "test-cred",
"access-token": "dsa",
"token-expiry": 123
}],
"default-credential": "test-cred"
}
}`), nil)
assert.Nil(err)

ctx, err := clictx.NewContext(context.Background(), cfg, "test")
Expand Down
17 changes: 17 additions & 0 deletions pkg/clicfg/clicfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clicfg
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -132,6 +133,20 @@ func (auraConfig *AuraConfig) SetDefaultCredential(name string) error {
return nil
}

func (auraConfig *AuraConfig) GetDefaultCredential() (AuraCredential, error) {
if auraConfig.DefaultCredential == "" {
return AuraCredential{}, errors.New("no default credential found")
}

for _, credential := range auraConfig.Credentials {
if credential.Name == auraConfig.DefaultCredential {
return credential, nil
}
}

return AuraCredential{}, fmt.Errorf("could not find credential with name %s", auraConfig.DefaultCredential)
}

func (auraConfig *AuraConfig) Print(cmd *cobra.Command) error {
encoder := json.NewEncoder(cmd.OutOrStdout())
encoder.SetIndent("", "\t")
Expand All @@ -147,6 +162,8 @@ type AuraCredential struct {
Name string `mapstructure:"name" json:"name"`
ClientId string `mapstructure:"client-id" json:"client-id"`
ClientSecret string `mapstructure:"client-secret" json:"client-secret"`
AccessToken string `mapstructure:"access-token" json:"access-token"`
TokenExpiry string `mapstructure:"token-expiry" json:"token-expiry"`
}

func NewConfig() (*Config, error) {
Expand Down

0 comments on commit c30a35d

Please sign in to comment.