From f6b91629536b6a6106659dc90e1f442ea932b2ec Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 7 Nov 2023 12:57:28 -0800 Subject: [PATCH] consistently expect snake case json values in token handler (#4015) --- .../internal/daemon/search_handler.go | 4 +-- .../internal/daemon/token_handler.go | 26 +++++++++---------- .../internal/daemon/token_handler_test.go | 16 ++++++------ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/internal/clientcache/internal/daemon/search_handler.go b/internal/clientcache/internal/daemon/search_handler.go index 56d67659a..559e1ccee 100644 --- a/internal/clientcache/internal/daemon/search_handler.go +++ b/internal/clientcache/internal/daemon/search_handler.go @@ -18,8 +18,8 @@ import ( // SearchResult is the struct returned to search requests. type SearchResult struct { - Targets []*targets.Target `json:",omitempty"` - Sessions []*sessions.Session `json:",omitempty"` + Targets []*targets.Target `json:"targets,omitempty"` + Sessions []*sessions.Session `json:"sessions,omitempty"` } const ( diff --git a/internal/clientcache/internal/daemon/token_handler.go b/internal/clientcache/internal/daemon/token_handler.go index fbdc62040..0dade912b 100644 --- a/internal/clientcache/internal/daemon/token_handler.go +++ b/internal/clientcache/internal/daemon/token_handler.go @@ -24,23 +24,23 @@ type refresher interface { // KeyringToken has keyring held auth token information. type KeyringToken struct { // The keyring type used by boundary to access the auth token - KeyringType string + KeyringType string `json:"keyring_type"` // The token identifier for the provided keyring type that holds the auth token - TokenName string + TokenName string `json:"token_name"` } // userTokenToAdd is the request body to this handler. type UpsertTokenRequest struct { // BoundaryAddr is a required field for all requests - BoundaryAddr string + BoundaryAddr string `json:"boundary_addr"` // The id of the auth token asserted to be attempted to be added - AuthTokenId string + AuthTokenId string `json:"auth_token_id"` // The raw auth token for this user. Either this field or the Keyring field // must be set but not both. - AuthToken string + AuthToken string `json:"auth_token"` // Keyring is the keyring info used when adding an auth token held in // keyring to the daemon. - Keyring *KeyringToken + Keyring *KeyringToken `json:"keyring"` } func newTokenHandlerFunc(ctx context.Context, repo *cache.Repository, refresher refresher) (http.HandlerFunc, error) { @@ -72,30 +72,30 @@ func newTokenHandlerFunc(ctx context.Context, repo *cache.Repository, refresher switch { case perReq.BoundaryAddr == "": - writeError(w, "BoundaryAddr is a required field but was empty", http.StatusBadRequest) + writeError(w, "boundary_addr is a required field but was empty", http.StatusBadRequest) return case perReq.AuthTokenId == "": - writeError(w, "AuthTokenId is a required field but was empty", http.StatusBadRequest) + writeError(w, "auth_token_id is a required field but was empty", http.StatusBadRequest) return case perReq.Keyring == nil && perReq.AuthToken == "": - writeError(w, "Either keyring info or the authtoken must be provided but were empty", http.StatusBadRequest) + writeError(w, "Either keyring info or the auth_token must be provided but were empty", http.StatusBadRequest) return case perReq.Keyring != nil: switch { case perReq.Keyring.TokenName == "": - writeError(w, "TokenName is a required field but was empty", http.StatusBadRequest) + writeError(w, "keyring.token_name is a required field but was empty", http.StatusBadRequest) return case perReq.Keyring.KeyringType == "": - writeError(w, "KeyringType is a required field but was empty", http.StatusBadRequest) + writeError(w, "keyring.keyring_type is a required field but was empty", http.StatusBadRequest) return case perReq.Keyring.KeyringType == base.NoneKeyring: - writeError(w, fmt.Sprintf("KeyringType is set to %s which is not supported", perReq.Keyring.KeyringType), http.StatusBadRequest) + writeError(w, fmt.Sprintf("keyring.keyring_type is set to %s which is not supported", perReq.Keyring.KeyringType), http.StatusBadRequest) return } case perReq.AuthToken != "": switch { case !strings.HasPrefix(perReq.AuthToken, perReq.AuthTokenId): - writeError(w, "The auth token id doesn't match the auth token's prefix", http.StatusBadRequest) + writeError(w, "auth_token_id doesn't match the auth_token's prefix", http.StatusBadRequest) return } } diff --git a/internal/clientcache/internal/daemon/token_handler_test.go b/internal/clientcache/internal/daemon/token_handler_test.go index 26c3b9367..f7fe57bbc 100644 --- a/internal/clientcache/internal/daemon/token_handler_test.go +++ b/internal/clientcache/internal/daemon/token_handler_test.go @@ -108,7 +108,7 @@ func TestKeyringToken(t *testing.T) { } apiErr := addToken(t, ph, pa) require.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "KeyringType is a required field but was empty") + assert.Contains(t, apiErr.Message, "keyring_type is a required field but was empty") assert.False(t, tr.called) }) @@ -123,7 +123,7 @@ func TestKeyringToken(t *testing.T) { } apiErr := addToken(t, ph, pa) require.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "KeyringType is set to none which is not supported") + assert.Contains(t, apiErr.Message, "keyring.keyring_type is set to none which is not supported") assert.False(t, tr.called) }) @@ -138,7 +138,7 @@ func TestKeyringToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "TokenName is a required field but was empty") + assert.Contains(t, apiErr.Message, "keyring.token_name is a required field but was empty") assert.False(t, tr.called) }) @@ -153,7 +153,7 @@ func TestKeyringToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "BoundaryAddr is a required field but was empty") + assert.Contains(t, apiErr.Message, "boundary_addr is a required field but was empty") assert.False(t, tr.called) }) @@ -168,7 +168,7 @@ func TestKeyringToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "AuthTokenId is a required field but was empty") + assert.Contains(t, apiErr.Message, "auth_token_id is a required field but was empty") assert.False(t, tr.called) }) @@ -234,7 +234,7 @@ func TestKeyringlessToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "BoundaryAddr is a required field but was empty") + assert.Contains(t, apiErr.Message, "boundary_addr is a required field but was empty") assert.False(t, tr.called) }) @@ -246,7 +246,7 @@ func TestKeyringlessToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "AuthTokenId is a required field but was empty") + assert.Contains(t, apiErr.Message, "auth_token_id is a required field but was empty") assert.False(t, tr.called) }) @@ -258,7 +258,7 @@ func TestKeyringlessToken(t *testing.T) { } apiErr := addToken(t, ph, pa) assert.NotNil(t, apiErr) - assert.Contains(t, apiErr.Message, "The auth token id doesn't match the auth token's prefix") + assert.Contains(t, apiErr.Message, "auth_token_id doesn't match the auth_token's prefix") assert.False(t, tr.called) })