Skip to content

Commit

Permalink
fix: unify ALI secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
Mryashbhardwaj committed Dec 31, 2024
1 parent bcbff22 commit 7ac17c6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 50 deletions.
26 changes: 13 additions & 13 deletions ext/bucket/oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ import (
)

type OSSCredentials struct {
AccessID string `json:"access_key_id"`
AccessKey string `json:"access_key_secret"`
Endpoint string `json:"endpoint"`
AccessID string `json:"access_id"`
AccessKey string `json:"access_key"`
Endpoint string `json:"oss_endpoint"`
ProjectName string `json:"project_name"`
Region string `json:"region"`
SecurityToken string `json:"security_token"`
}

func getOSSCredentials(jsonData string) (*OSSCredentials, error) {
var creds OSSCredentials
if err := json.Unmarshal([]byte(jsonData), &creds); err != nil {
return &OSSCredentials{}, err
}

return &creds, nil
}

func NewOssClient(creds string) (*oss.Client, error) {
cred, err := toOSSCredentials(creds)
cred, err := getOSSCredentials(creds)
if err != nil {
return nil, err
}
Expand All @@ -35,12 +44,3 @@ func NewOssClient(creds string) (*oss.Client, error) {

return oss.NewClient(cfg), nil
}

func toOSSCredentials(creds string) (OSSCredentials, error) {
var cred OSSCredentials
if err := json.Unmarshal([]byte(creds), &cred); err != nil {
return OSSCredentials{}, err
}

return cred, nil
}
31 changes: 15 additions & 16 deletions ext/store/maxcompute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,27 @@ func (MaxComputeClientProvider) Get(account string) (Client, error) {
return NewClient(account)
}

type MaxComputeClient struct {
*odps.Odps
}

type maxComputeCredentials struct {
type MaxComputeCredentials struct {
AccessID string `json:"access_id"`
AccessKey string `json:"access_key"`
Endpoint string `json:"endpoint"`
Endpoint string `json:"mc_endpoint"`
ProjectName string `json:"project_name"`
}

func getMaxComputeCredential(jsonData string) (*MaxComputeCredentials, error) {
var creds MaxComputeCredentials
if err := json.Unmarshal([]byte(jsonData), &creds); err != nil {
return nil, err
}
return &creds, nil
}

type MaxComputeClient struct {
*odps.Odps
}

func NewClient(svcAccount string) (*MaxComputeClient, error) {
cred, err := collectMaxComputeCredential([]byte(svcAccount))
cred, err := getMaxComputeCredential(svcAccount)
if err != nil {
return nil, errors.InternalError(store, "failed to read account", err)
}
Expand Down Expand Up @@ -82,12 +90,3 @@ func (c *MaxComputeClient) GetDDLView(_ context.Context, table string) (string,
}
return "", nil
}

func collectMaxComputeCredential(jsonData []byte) (*maxComputeCredentials, error) {
var creds maxComputeCredentials
if err := json.Unmarshal(jsonData, &creds); err != nil {
return nil, err
}

return &creds, nil
}
2 changes: 1 addition & 1 deletion ext/store/maxcompute/maxcompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
accountKey = "DATASTORE_MAXCOMPUTE"
accountKey = "ALI_CREDS"
store = "MaxComputeStore"

maxcomputeID = "maxcompute"
Expand Down
38 changes: 19 additions & 19 deletions ext/store/maxcompute/maxcompute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMaxComputeStore(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Run("returns error when secret is not provided", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(nil, errors.New("not found secret"))
defer secretProvider.AssertExpectations(t)

Expand All @@ -46,7 +46,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when not able to get client", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -64,7 +64,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when schema name is empty", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -85,7 +85,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when kind is invalid", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -107,7 +107,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("return success when calls appropriate handler for table", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -132,7 +132,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("return success when calls appropriate handler for view", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -159,7 +159,7 @@ func TestMaxComputeStore(t *testing.T) {
t.Run("Update", func(t *testing.T) {
t.Run("returns error when secret is not provided", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(nil, errors.New("not found secret"))
defer secretProvider.AssertExpectations(t)

Expand All @@ -175,7 +175,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when not able to get client", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -193,7 +193,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when schema name is empty", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -214,7 +214,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when kind is invalid", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -236,7 +236,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("return success when calls appropriate handler for table", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -261,7 +261,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("return success when calls appropriate handler for view", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand Down Expand Up @@ -383,7 +383,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns false and error when secret is not provided", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(nil, errors.New("not found secret"))
defer secretProvider.AssertExpectations(t)

Expand All @@ -401,7 +401,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns false and error when not able to get client", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").Return(pts, nil)
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").Return(pts, nil)
defer secretProvider.AssertExpectations(t)

clientProvider := new(mockClientProvider)
Expand All @@ -419,7 +419,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns error when schema name is empty", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").
Return(pts, nil)
defer secretProvider.AssertExpectations(t)

Expand All @@ -441,7 +441,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns true and error when resource name is invalid", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").Return(pts, nil)
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").Return(pts, nil)
defer secretProvider.AssertExpectations(t)

client := new(mockClient)
Expand All @@ -465,7 +465,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns true and error when resource name is empty", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").Return(pts, nil)
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").Return(pts, nil)
defer secretProvider.AssertExpectations(t)

client := new(mockClient)
Expand All @@ -489,7 +489,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns true and nil when schema table resource does exist", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").Return(pts, nil)
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").Return(pts, nil)
defer secretProvider.AssertExpectations(t)

client := new(mockClient)
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestMaxComputeStore(t *testing.T) {
})
t.Run("returns false and nil when schema table resource does not exist", func(t *testing.T) {
secretProvider := new(mockSecretProvider)
secretProvider.On("GetSecret", mock.Anything, tnnt, "DATASTORE_MAXCOMPUTE").Return(pts, nil)
secretProvider.On("GetSecret", mock.Anything, tnnt, "ALI_CREDS").Return(pts, nil)
defer secretProvider.AssertExpectations(t)

client := new(mockClient)
Expand Down
2 changes: 1 addition & 1 deletion ext/store/maxcompute/sheet_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const (
GsheetCredsKey = "GOOGLE_SHEETS_ACCOUNT"
OSSCredsKey = "OSS_CREDS"
OSSCredsKey = "ALI_CREDS"
ExtLocation = ""
putTimeOut = time.Second * 10
)
Expand Down

0 comments on commit 7ac17c6

Please sign in to comment.