Skip to content

Commit

Permalink
Lint related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kinjelom committed Aug 26, 2024
1 parent 1e20962 commit 65224a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
23 changes: 15 additions & 8 deletions internal/keystore/credhub/credhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
applicationJSON = "application/json"
)

// Config holds the configuration settings for connecting to a CredHub service.
type Config struct {
BaseURL string // The base URL endpoint of the CredHub service.
EnableMutualTLS bool // If set to true, enables mutual TLS.
Expand All @@ -40,11 +41,14 @@ type Config struct {
ForceBase64ValuesEncoding bool // If set to true, forces encoding of all the values as base64 before storage.
}

// Certs contains the certificates needed for mutual TLS authentication.
type Certs struct {
ServerCaCert *x509.Certificate
ClientKeyPair tls.Certificate
}

// Validate checks the configuration for correctness and loads the necessary certificates for mutual TLS authentication.
// It returns a Certs object containing the server CA certificate and client key pair, or an error if validation fails.
func (c *Config) Validate() (*Certs, error) {
certs := &Certs{}
if c.BaseURL == "" {
Expand Down Expand Up @@ -93,22 +97,25 @@ func (c *Config) Validate() (*Certs, error) {
func (c *Config) validatePemFile(path, name string) (pemBytes, derBytes []byte, err error) {
pemBytes, err = os.ReadFile(path)
if err != nil {
return pemBytes, nil, errors.New(fmt.Sprintf("credhub config: failed to load PEM file '%s'='%s': %v", name, path, err))
return pemBytes, nil, fmt.Errorf("credhub config: failed to load PEM file '%s'='%s': %v", name, path, err)
}
derBlock, _ := pem.Decode(pemBytes)
if derBlock == nil {
return pemBytes, nil, errors.New(fmt.Sprintf("credhub config: failed to decode the '%s'='%s' from PEM format, no PEM data found", name, path))
return pemBytes, nil, fmt.Errorf("credhub config: failed to decode the '%s'='%s' from PEM format, no PEM data found", name, path)
}
return pemBytes, derBlock.Bytes, nil
}

// Store represents a layer that interacts with a CredHub service using HTTP protocol.
type Store struct {
LastError error
config *Config
client httpClient
sfGroup singleflight.Group
}

// NewStore creates a new instance of Store, initializing it with the provided configuration.
// It returns an error if the HTTP client initialization fails.
func NewStore(_ context.Context, config *Config) (*Store, error) {
client, err := newHTTPMTLSClient(config)
if err != nil {
Expand Down Expand Up @@ -140,12 +147,12 @@ func (s *Store) Status(ctx context.Context) (kes.KeyStoreState, error) {
}
if err := json.NewDecoder(resp.body).Decode(&responseData); err != nil {
return state, fmt.Errorf("failed to parse response: %v", err)
} else {
if responseData.Status == "UP" {
return state, nil
}
return state, fmt.Errorf("CredHub is not UP, status: %s", responseData.Status)
}
if responseData.Status == "UP" {
return state, nil
}
return state, fmt.Errorf("CredHub is not UP, status: %s", responseData.Status)

}
return state, fmt.Errorf("the CredHub (%s) is not healthy, status: %s", uri, resp.status)
}
Expand Down Expand Up @@ -179,7 +186,7 @@ func (s *Store) create(ctx context.Context, name string, value []byte, operation
// - `credhub curl -X=PUT -p "/api/v1/data" -d='{"name":"/test-namespace/key-1","type":"value","value":"1"}`
func (s *Store) put(ctx context.Context, name string, value []byte, operationID string) error {
uri := "/api/v1/data"
valueStr := bytesToJsonString(value, s.config.ForceBase64ValuesEncoding)
valueStr := bytesToJSONString(value, s.config.ForceBase64ValuesEncoding)
data := map[string]interface{}{
"name": s.config.Namespace + "/" + name,
"type": "value",
Expand Down
2 changes: 1 addition & 1 deletion internal/keystore/credhub/credhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestStore_Get(t *testing.T) {
t.Run("GET bytes value with Base64 encoding request contract", func(t *testing.T) {
const key = "key"
value := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 80, 114, 122, 255, 121, 107, 108, 255}
encodedValue := bytesToJsonString(value, true)
encodedValue := bytesToJSONString(value, true)
fakeClient.respStatusCodes["GET"] = 200
fakeClient.respBody = fmt.Sprintf(`
{
Expand Down
12 changes: 6 additions & 6 deletions internal/keystore/credhub/value_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"unicode/utf8"
)

const Base64Prefix = "Base64:"
const base64Prefix = "Base64:"

func bytesToJsonString(bytes []byte, forceBase64 bool) (value string) {
func bytesToJSONString(bytes []byte, forceBase64 bool) (value string) {
if utf8.Valid(bytes) && !forceBase64 {
strBytes := string(bytes)
if !strings.HasPrefix(strBytes, Base64Prefix) {
if !strings.HasPrefix(strBytes, base64Prefix) {
return string(bytes)
}
}
return Base64Prefix + base64.StdEncoding.EncodeToString(bytes)
return base64Prefix + base64.StdEncoding.EncodeToString(bytes)
}

func jsonStringToBytes(value string) (bytes []byte, err error) {
if strings.HasPrefix(value, Base64Prefix) {
return base64.StdEncoding.DecodeString(strings.TrimPrefix(value, Base64Prefix))
if strings.HasPrefix(value, base64Prefix) {
return base64.StdEncoding.DecodeString(strings.TrimPrefix(value, base64Prefix))
}
return []byte(value), nil
}

0 comments on commit 65224a6

Please sign in to comment.