Skip to content

feat(api): validate required app config items #2472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,17 +881,22 @@ func TestLinuxPatchAppConfigValues(t *testing.T) {

// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check request method and path
assert.Equal(t, "PATCH", r.Method)
assert.Equal(t, "/api/linux/install/app/config/values", r.URL.Path)

// Check headers
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization"))

// Decode and verify request body
// Decode request body
var req types.PatchAppConfigValuesRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
require.NoError(t, err, "Failed to decode request body")

// Verify the request contains expected values
assert.Equal(t, "new-value", req.Values["test-item"])
assert.Equal(t, "required-value", req.Values["required-item"])

// Return successful response
w.WriteHeader(http.StatusOK)
Expand All @@ -901,32 +906,33 @@ func TestLinuxPatchAppConfigValues(t *testing.T) {

// Test successful set
c := New(server.URL, WithToken("test-token"))
values := map[string]string{
"test-item": "new-value",
configValues := map[string]string{
"test-item": "new-value",
"required-item": "required-value",
}
config, err := c.PatchLinuxAppConfigValues(values)
assert.NoError(t, err)
config, err := c.PatchLinuxAppConfigValues(configValues)
require.NoError(t, err)
assert.Equal(t, expectedConfig, config)

// Test error response
errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(types.APIError{
StatusCode: http.StatusInternalServerError,
Message: "Internal Server Error",
StatusCode: http.StatusBadRequest,
Message: "Bad Request",
})
}))
defer errorServer.Close()

c = New(errorServer.URL, WithToken("test-token"))
config, err = c.PatchLinuxAppConfigValues(values)
config, err = c.PatchLinuxAppConfigValues(configValues)
assert.Error(t, err)
assert.Equal(t, types.AppConfig{}, config)

apiErr, ok := err.(*types.APIError)
require.True(t, ok, "Expected err to be of type *types.APIError")
assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode)
assert.Equal(t, "Internal Server Error", apiErr.Message)
assert.Equal(t, http.StatusBadRequest, apiErr.StatusCode)
assert.Equal(t, "Bad Request", apiErr.Message)
}

func TestKubernetesPatchAppConfigValues(t *testing.T) {
Expand All @@ -951,17 +957,22 @@ func TestKubernetesPatchAppConfigValues(t *testing.T) {

// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check request method and path
assert.Equal(t, "PATCH", r.Method)
assert.Equal(t, "/api/kubernetes/install/app/config/values", r.URL.Path)

// Check headers
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization"))

// Decode and verify request body
// Decode request body
var req types.PatchAppConfigValuesRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
require.NoError(t, err, "Failed to decode request body")

// Verify the request contains expected values
assert.Equal(t, "new-value", req.Values["test-item"])
assert.Equal(t, "required-value", req.Values["required-item"])

// Return successful response
w.WriteHeader(http.StatusOK)
Expand All @@ -971,30 +982,31 @@ func TestKubernetesPatchAppConfigValues(t *testing.T) {

// Test successful set
c := New(server.URL, WithToken("test-token"))
values := map[string]string{
"test-item": "new-value",
configValues := map[string]string{
"test-item": "new-value",
"required-item": "required-value",
}
config, err := c.PatchKubernetesAppConfigValues(values)
config, err := c.PatchKubernetesAppConfigValues(configValues)
assert.NoError(t, err)
assert.Equal(t, expectedConfig, config)

// Test error response
errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(types.APIError{
StatusCode: http.StatusInternalServerError,
Message: "Internal Server Error",
StatusCode: http.StatusBadRequest,
Message: "Bad Request",
})
}))
defer errorServer.Close()

c = New(errorServer.URL, WithToken("test-token"))
config, err = c.PatchKubernetesAppConfigValues(values)
config, err = c.PatchKubernetesAppConfigValues(configValues)
assert.Error(t, err)
assert.Equal(t, types.AppConfig{}, config)

apiErr, ok := err.(*types.APIError)
require.True(t, ok, "Expected err to be of type *types.APIError")
assert.Equal(t, http.StatusInternalServerError, apiErr.StatusCode)
assert.Equal(t, "Internal Server Error", apiErr.Message)
assert.Equal(t, http.StatusBadRequest, apiErr.StatusCode)
assert.Equal(t, "Bad Request", apiErr.Message)
}
5 changes: 3 additions & 2 deletions api/client/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,11 @@ func (c *client) GetKubernetesAppConfigValues() (map[string]string, error) {
}

func (c *client) PatchKubernetesAppConfigValues(values map[string]string) (types.AppConfig, error) {
req := types.PatchAppConfigValuesRequest{
request := types.PatchAppConfigValuesRequest{
Values: values,
}
b, err := json.Marshal(req)

b, err := json.Marshal(request)
if err != nil {
return types.AppConfig{}, err
}
Expand Down
9 changes: 7 additions & 2 deletions api/controllers/kubernetes/install/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func (c *InstallController) PatchAppConfigValues(ctx context.Context, values map
}
}()

err = c.appConfigManager.PatchConfigValues(ctx, *c.releaseData.AppConfig, values)
err = c.appConfigManager.ValidateConfigValues(*c.releaseData.AppConfig, values)
if err != nil {
return fmt.Errorf("validate app config values: %w", err)
}

err = c.appConfigManager.PatchConfigValues(*c.releaseData.AppConfig, values)
if err != nil {
return fmt.Errorf("patch app config values: %w", err)
}
Expand All @@ -71,5 +76,5 @@ func (c *InstallController) GetAppConfigValues(ctx context.Context, maskPassword
return nil, fmt.Errorf("get app config: %w", err)
}

return c.appConfigManager.GetConfigValues(ctx, appConfig, maskPasswords)
return c.appConfigManager.GetConfigValues(appConfig, maskPasswords)
}
Loading
Loading