From 5adf32225d3863ae40fd351ea33629f3fcca2447 Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Tue, 19 Mar 2024 11:26:27 +0300 Subject: [PATCH] fix url building logic --- secretsmanager.go | 4 ++-- service/certs/certs.go | 24 +++++++++++++----------- service/secrets/secrets.go | 20 +++++++++++++++----- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/secretsmanager.go b/secretsmanager.go index e91bc79..74ebd47 100644 --- a/secretsmanager.go +++ b/secretsmanager.go @@ -12,10 +12,10 @@ import ( const ( // URL for working with secrets. - defaultAPIURLSecrets = "https://cloud.api.selcloud.ru/secrets-manager/v1/" //nolint:gosec + defaultAPIURLSecrets = "https://cloud.api.selcloud.ru/secrets-manager/" //nolint:gosec // URL for working with certificates. - defaultAPIURLUserCertificates = "https://cloud.api.selcloud.ru/certificate-manager/v1/" + defaultAPIURLUserCertificates = "https://cloud.api.selcloud.ru/certificate-manager/" ) // Client — implements operations to work with the Secrets Manager API using the Keystone Token. diff --git a/service/certs/certs.go b/service/certs/certs.go index a9bd422..9ed4b7e 100644 --- a/service/certs/certs.go +++ b/service/certs/certs.go @@ -11,6 +11,8 @@ import ( "github.com/selectel/secretsmanager-go/secretsmanagererrors" ) +const apiVersion = "v1" + // Service implements Secrets Manager that is responsible for handling certificates operations. type Service struct { apiURLUserCertificates string @@ -32,7 +34,7 @@ func (s Service) Delete(ctx context.Context, id string) error { } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id) + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -56,7 +58,7 @@ func (s Service) Get(ctx context.Context, id string) (Certificate, error) { } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id) + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id) if err != nil { return Certificate{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -93,7 +95,7 @@ func (s Service) UpdateVersion(ctx context.Context, id string, pem UpdateCertifi return err } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id) + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -133,7 +135,7 @@ func (s Service) UpdateName(ctx context.Context, id, name string) error { } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id) + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -171,7 +173,7 @@ func (s Service) GetPublicCerts(ctx context.Context, id string) (string, error) } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id, "ca_chain") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id, "ca_chain") if err != nil { return "", secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -195,7 +197,7 @@ func (s Service) RemoveConsumers(ctx context.Context, id string, consumers Remov } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id, "consumers") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id, "consumers") if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -229,7 +231,7 @@ func (s Service) AddConsumers(ctx context.Context, id string, consumers AddConsu } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id, "consumers") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id, "consumers") if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -263,7 +265,7 @@ func (s Service) GetPKCS12Bundle(ctx context.Context, id string) ([]byte, error) } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id, "p12") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id, "p12") if err != nil { return []byte{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -287,7 +289,7 @@ func (s Service) GetPrivateKey(ctx context.Context, id string) (string, error) { } } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "cert", id, "private_key") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "cert", id, "private_key") if err != nil { return "", secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -304,7 +306,7 @@ func (s Service) GetPrivateKey(ctx context.Context, id string) (string, error) { } func (s Service) List(ctx context.Context) (GetCertificatesResponse, error) { - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "certs") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "certs") if err != nil { return GetCertificatesResponse{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -334,7 +336,7 @@ func (s Service) Create(ctx context.Context, ucr CreateCertificateRequest) (Cert return Certificate{}, err } - endpoint, err := url.JoinPath(s.apiURLUserCertificates, "certs") + endpoint, err := url.JoinPath(s.apiURLUserCertificates, apiVersion, "certs") if err != nil { return Certificate{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, diff --git a/service/secrets/secrets.go b/service/secrets/secrets.go index 1c0a92e..1daf0db 100644 --- a/service/secrets/secrets.go +++ b/service/secrets/secrets.go @@ -12,6 +12,8 @@ import ( "github.com/selectel/secretsmanager-go/secretsmanagererrors" ) +const apiVersion = "v1" + // Service implements Secrets Manager part that is responsible for handling secrets operations. type Service struct { apiURLSecrets string @@ -26,7 +28,15 @@ func New(url string, client *httpclient.HTTPClient) *Service { } func (s Service) List(ctx context.Context) (Secrets, error) { - endpoint, err := url.Parse(s.apiURLSecrets) + rawEndpoint, err := url.JoinPath(s.apiURLSecrets, apiVersion) + if err != nil { + return Secrets{}, secretsmanagererrors.Error{ + Err: secretsmanagererrors.ErrCannotFormatEndpoint, + Desc: err.Error(), + } + } + + endpoint, err := url.Parse(rawEndpoint) if err != nil { return Secrets{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -62,7 +72,7 @@ func (s Service) Delete(ctx context.Context, key string) error { } } - endpoint, err := url.JoinPath(s.apiURLSecrets, key) + endpoint, err := url.JoinPath(s.apiURLSecrets, apiVersion, key) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrInternalAppError, @@ -85,7 +95,7 @@ func (s Service) Get(ctx context.Context, key string) (Secret, error) { } } - endpoint, err := url.JoinPath(s.apiURLSecrets, key) + endpoint, err := url.JoinPath(s.apiURLSecrets, apiVersion, key) if err != nil { return Secret{}, secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -118,7 +128,7 @@ func (s Service) Update(ctx context.Context, usc UserSecret) error { } } - endpoint, err := url.JoinPath(s.apiURLSecrets, usc.Key) + endpoint, err := url.JoinPath(s.apiURLSecrets, apiVersion, usc.Key) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint, @@ -160,7 +170,7 @@ func (s Service) Create(ctx context.Context, usc UserSecret) error { } usc.Value = base64.StdEncoding.EncodeToString([]byte(usc.Value)) - endpoint, err := url.JoinPath(s.apiURLSecrets, usc.Key) + endpoint, err := url.JoinPath(s.apiURLSecrets, apiVersion, usc.Key) if err != nil { return secretsmanagererrors.Error{ Err: secretsmanagererrors.ErrCannotFormatEndpoint,