Skip to content

Commit

Permalink
fix url building logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterZurg authored Mar 19, 2024
1 parent b8f0a8e commit 5adf322
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 13 additions & 11 deletions service/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions service/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5adf322

Please sign in to comment.