Skip to content

Commit

Permalink
chore: client upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
s1ntaxe770r authored and andrew-s committed Jun 5, 2024
1 parent 973d36d commit 2ecbe60
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"

"github.com/joho/godotenv"
"github.com/qernal/cli-qernal/pkg/oauth"

"github.com/hashicorp/terraform-plugin-log/tflog"
openapiclient "github.com/qernal/openapi-chaos-go-client"
"golang.org/x/crypto/nacl/box"
)

var (
host_hydra = "https://hydra.qernal-bld.dev"
host_chaos = "https://chaos.qernal-bld.dev"
hostHydra = getEnv("HOST_HYDRA", "https://hydra.qernal.dev")
hostChaos = getEnv("HOST_CHAOS", "https://chaos.qernal.dev")
)

type QernalAPIClient struct {
Expand All @@ -27,7 +29,7 @@ type QernalAPIClient struct {

func New(ctx context.Context, token string) (client QernalAPIClient, err error) {

oauthClient := oauth.NewOauthClient(host_hydra)
oauthClient := oauth.NewOauthClient(hostHydra)
err = oauthClient.ExtractClientIDAndClientSecretFromToken(token)
if err != nil {
return QernalAPIClient{}, err
Expand All @@ -41,7 +43,7 @@ func New(ctx context.Context, token string) (client QernalAPIClient, err error)
configuration := &openapiclient.Configuration{
Servers: openapiclient.ServerConfigurations{
{
URL: host_chaos + "/v1",
URL: hostChaos + "/v1",
},
},
DefaultHeader: map[string]string{
Expand All @@ -57,12 +59,11 @@ func New(ctx context.Context, token string) (client QernalAPIClient, err error)

func (qc *QernalAPIClient) FetchDek(ctx context.Context, projectID string) (*openapiclient.SecretMetaResponse, error) {
keyRes, httpres, err := qc.SecretsAPI.ProjectsSecretsGet(ctx, projectID, "dek").Execute()
slog.Info(httpres.Status)
if err != nil {
resData, httperr := ParseResponseData(httpres)
ctx = tflog.SetField(ctx, "http response", httperr)
tflog.Error(ctx, "response from server")
if httperr != nil {
return nil, fmt.Errorf("failed to fetch DEK key: unexpected HTTP error: %w", err)
return nil, fmt.Errorf("failed to fetch DEK key: unexpected HTTP error: %w", httperr)
}
return nil, fmt.Errorf("failed to fetch DEK key: unexpected error: %w, detail: %v", err, resData)
}
Expand Down Expand Up @@ -110,3 +111,15 @@ func EncryptLocalSecret(pk, secret string) (string, error) {

return base64.StdEncoding.EncodeToString(encrypted), nil
}

func getEnv(key, defaultValue string) string {
err := godotenv.Load()
if err != nil {
slog.Debug("falling back to default ")
}

if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}

0 comments on commit 2ecbe60

Please sign in to comment.