Skip to content

Commit

Permalink
chore: support cloud api key name and secret
Browse files Browse the repository at this point in the history
  • Loading branch information
ldming committed Oct 31, 2024
1 parent 2b43434 commit 8727b06
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .generator/schemas/adminapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ info:
altText: ApeCloud logo
description: |
The Admin API is used to manage the ApeCloud platform.
contact:
email: [email protected]
name: ApeCloud Support
url: https://www.apecloud.com
servers:
- url: http://127.0.0.1:8080
description: local
Expand Down
4 changes: 4 additions & 0 deletions .generator/schemas/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ info:
We use standard HTTP authentication and provide API keys to identify who you are. Your API keys carry many privileges, so be sure to keep them secret! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth.
All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail.
contact:
email: [email protected]
name: ApeCloud Support
url: https://www.apecloud.com
servers:
- url: http://127.0.0.1:8080
description: local
Expand Down
9 changes: 9 additions & 0 deletions .generator/src/generator/templates/client.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"
"unicode/utf8"

"github.com/icholy/digest"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -389,6 +390,14 @@ func (c *APIClient) PrepareRequest(
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
}

// Digest Authentication
if auth, ok := ctx.Value(ContextDigestAuth).(DigestAuth); ok {
c.Cfg.HTTPClient.Transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
}
}
}

for header, value := range c.Cfg.DefaultHeader {
Expand Down
22 changes: 17 additions & 5 deletions .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ var (

// ContextOperationServerVariables overrides a server configuration variables using operation specific values.
ContextOperationServerVariables = contextKey("serverOperationVariables")

// ContextDigestAuth takes DigestAuth as authentication for the request.
ContextDigestAuth = contextKey("digest")
)

// DigestAuth provides digest http authentication to a request passed via context using ContextDigestAuth.
type DigestAuth struct {
UserName string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth.
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Expand Down Expand Up @@ -364,14 +373,17 @@ func NewDefaultContext(ctx context.Context) context.Context {
)
}

keys := make(map[string]APIKey)
if apiKey, ok := os.LookupEnv("KB_CLOUD_API_KEY"); ok {
keys["apiKeyAuth"] = APIKey{Key: apiKey}
auth := DigestAuth{}
if keyName, ok := os.LookupEnv("KB_CLOUD_API_KEY_NAME"); ok {
auth.UserName = keyName
}
if keySecret, ok := os.LookupEnv("KB_CLOUD_API_KEY_SECRET"); ok {
auth.Password = keySecret
}
ctx = context.WithValue(
ctx,
ContextAPIKeys,
keys,
ContextDigestAuth,
auth,
)

return ctx
Expand Down
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch test function",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/tests/api/api_organization_test.go",
"args": [
"-test.run",
"TestListOrgs"
],
"envFile": "${workspaceFolder}/.env",
"showLog": false,
}
]
}
9 changes: 9 additions & 0 deletions api/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

"github.com/icholy/digest"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -389,6 +390,14 @@ func (c *APIClient) PrepareRequest(
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
}

// Digest Authentication
if auth, ok := ctx.Value(ContextDigestAuth).(DigestAuth); ok {
c.Cfg.HTTPClient.Transport = &digest.Transport{
Username: auth.UserName,
Password: auth.Password,
}
}
}

for header, value := range c.Cfg.DefaultHeader {
Expand Down
22 changes: 17 additions & 5 deletions api/common/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ var (

// ContextOperationServerVariables overrides a server configuration variables using operation specific values.
ContextOperationServerVariables = contextKey("serverOperationVariables")

// ContextDigestAuth takes DigestAuth as authentication for the request.
ContextDigestAuth = contextKey("digest")
)

// DigestAuth provides digest http authentication to a request passed via context using ContextDigestAuth.
type DigestAuth struct {
UserName string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth.
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Expand Down Expand Up @@ -323,14 +332,17 @@ func NewDefaultContext(ctx context.Context) context.Context {
)
}

keys := make(map[string]APIKey)
if apiKey, ok := os.LookupEnv("KB_CLOUD_API_KEY"); ok {
keys["apiKeyAuth"] = APIKey{Key: apiKey}
auth := DigestAuth{}
if keyName, ok := os.LookupEnv("KB_CLOUD_API_KEY_NAME"); ok {
auth.UserName = keyName
}
if keySecret, ok := os.LookupEnv("KB_CLOUD_API_KEY_SECRET"); ok {
auth.Password = keySecret
}
ctx = context.WithValue(
ctx,
ContextAPIKeys,
keys,
ContextDigestAuth,
auth,
)

return ctx
Expand Down
70 changes: 70 additions & 0 deletions api/kbcloud/model_load_balancer_available_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at ApeCloud (https://www.apecloud.com/).
// Copyright 2022-Present ApeCloud Co., Ltd

package kbcloud

import (
"fmt"

"github.com/apecloud/kb-cloud-client-go/api/common"
)

// LoadBalancerAvailableType Whether the loadbalancer is available in the environment.
type LoadBalancerAvailableType string

// List of LoadBalancerAvailableType.
const (
LoadBalancerAvailableTypeAvailable LoadBalancerAvailableType = "Available"
LoadBalancerAvailableTypeUnavailable LoadBalancerAvailableType = "Unavailable"
LoadBalancerAvailableTypeChecking LoadBalancerAvailableType = "Checking"
LoadBalancerAvailableTypeUnknown LoadBalancerAvailableType = "Unknown"
)

var allowedLoadBalancerAvailableTypeEnumValues = []LoadBalancerAvailableType{
LoadBalancerAvailableTypeAvailable,
LoadBalancerAvailableTypeUnavailable,
LoadBalancerAvailableTypeChecking,
LoadBalancerAvailableTypeUnknown,
}

// GetAllowedValues returns the list of possible values.
func (v *LoadBalancerAvailableType) GetAllowedValues() []LoadBalancerAvailableType {
return allowedLoadBalancerAvailableTypeEnumValues
}

// UnmarshalJSON deserializes the given payload.
func (v *LoadBalancerAvailableType) UnmarshalJSON(src []byte) error {
var value string
err := common.Unmarshal(src, &value)
if err != nil {
return err
}
*v = LoadBalancerAvailableType(value)
return nil
}

// NewLoadBalancerAvailableTypeFromValue returns a pointer to a valid LoadBalancerAvailableType
// for the value passed as argument, or an error if the value passed is not allowed by the enum.
func NewLoadBalancerAvailableTypeFromValue(v string) (*LoadBalancerAvailableType, error) {
ev := LoadBalancerAvailableType(v)
if ev.IsValid() {
return &ev, nil
}
return nil, fmt.Errorf("invalid value '%v' for LoadBalancerAvailableType: valid values are %v", v, allowedLoadBalancerAvailableTypeEnumValues)
}

// IsValid return true if the value is valid for the enum, false otherwise.
func (v LoadBalancerAvailableType) IsValid() bool {
for _, existing := range allowedLoadBalancerAvailableTypeEnumValues {
if existing == v {
return true
}
}
return false
}

// Ptr returns reference to LoadBalancerAvailableType value.
func (v LoadBalancerAvailableType) Ptr() *LoadBalancerAvailableType {
return &v
}
11 changes: 11 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -e
set -x

cd .generator
poetry install
poetry run python -m generator ./schemas/* -o ../api

cd ../api
goimports -w .
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/goccy/go-json v0.10.2
github.com/google/uuid v1.5.0
golang.org/x/oauth2 v0.10.0
github.com/icholy/digest v0.1.23
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/icholy/digest v0.1.23 h1:4hX2pIloP0aDx7RJW0JewhPPy3R8kU+vWKdxPsCCGtY=
github.com/icholy/digest v0.1.23/go.mod h1:QNrsSGQ5v7v9cReDI0+eyjsXGUoRSUZQHeQ5C4XLa0Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
Expand Down
2 changes: 1 addition & 1 deletion tests/api/api_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCreateCluster(t *testing.T) {
Version: common.PtrString("8.4.2"),
Namespace: common.PtrString("kubeblocks-cloud-ns"),
SingleZone: common.PtrBool(true),
TerminationPolicy: common.Ptr(kbcloud.CLUSTERTERMINATIONPOLICY_DELETE),
TerminationPolicy: common.Ptr(kbcloud.ClusterTerminationPolicyDelete),
ProxyEnabled: common.PtrBool(true),
EnvironmentName: "test",
Components: []kbcloud.ComponentsItem{
Expand Down
22 changes: 22 additions & 0 deletions tests/api/api_organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test

import (
"context"
"net/http"
"testing"

"github.com/apecloud/kb-cloud-client-go/api/kbcloud"
"github.com/apecloud/kb-cloud-client-go/tests"
)

func TestListOrgs(t *testing.T) {
ctx := WithClient(WithTestAuth(NewDefaultContext(context.Background())))
assert := tests.Assert(ctx, t)
client := Client(ctx)
api := kbcloud.NewOrganizationApi(client)

orgs, resp, err := api.ListOrg(ctx)
assert.NoError(err)
assert.Equal(http.StatusOK, resp.StatusCode)
assert.NotNil(orgs)
}
15 changes: 4 additions & 11 deletions tests/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,10 @@ func WithFakeAuth(ctx context.Context) context.Context {
func WithTestAuth(ctx context.Context) context.Context {
return context.WithValue(
ctx,
common.ContextAPIKeys,
map[string]common.APIKey{
"BearerToken": {
Key: os.Getenv("KB_CLOUD_TEST_CLIENT_BEARER_TOKEN"),
},
"apiKeyAuth": {
Key: os.Getenv("KB_CLOUD_TEST_CLIENT_API_KEY"),
},
"appKeyAuth": {
Key: os.Getenv("KB_CLOUD_TEST_CLIENT_APP_KEY"),
},
common.ContextDigestAuth,
common.DigestAuth{
UserName: os.Getenv("KB_CLOUD_TEST_API_KEY_NAME"),
Password: os.Getenv("KB_CLOUD_TEST_API_KEY_SECRET"),
},
)
}
Expand Down
3 changes: 2 additions & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/icholy/digest v0.1.23 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
Expand All @@ -56,4 +57,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/apecloud/kb-cloud-client-go => ../
replace github.com/apecloud/kb-cloud-client-go => ../
2 changes: 2 additions & 0 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25L
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icholy/digest v0.1.23 h1:4hX2pIloP0aDx7RJW0JewhPPy3R8kU+vWKdxPsCCGtY=
github.com/icholy/digest v0.1.23/go.mod h1:QNrsSGQ5v7v9cReDI0+eyjsXGUoRSUZQHeQ5C4XLa0Y=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down

0 comments on commit 8727b06

Please sign in to comment.