-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2311 from posit-dev/dotnomad/get-secrets-api
Add GET configuration secrets endpoint
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package api | ||
|
||
// Copyright (C) 2024 by Posit Software, PBC. | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/posit-dev/publisher/internal/config" | ||
"github.com/posit-dev/publisher/internal/logging" | ||
"github.com/posit-dev/publisher/internal/types" | ||
"github.com/posit-dev/publisher/internal/util" | ||
) | ||
|
||
func GetConfigSecretsHandlerFunc(base util.AbsolutePath, log logging.Logger) http.HandlerFunc { | ||
return func(w http.ResponseWriter, req *http.Request) { | ||
name := mux.Vars(req)["name"] | ||
|
||
projectDir, _, err := ProjectDirFromRequest(base, w, req, log) | ||
if err != nil { | ||
// Response already returned by ProjectDirFromRequest | ||
return | ||
} | ||
|
||
configPath := config.GetConfigPath(projectDir, name) | ||
cfg, err := configFromFile(configPath) | ||
if err != nil { | ||
if aerr, ok := err.(*types.AgentError); ok { | ||
if aerr.Code == types.ErrorUnknownTOMLKey { | ||
apiErr := APIErrorUnknownTOMLKeyFromAgentError(*aerr) | ||
apiErr.JSONResponse(w) | ||
return | ||
} | ||
|
||
if aerr.Code == types.ErrorInvalidTOML { | ||
apiErr := APIErrorInvalidTOMLFileFromAgentError(*aerr) | ||
apiErr.JSONResponse(w) | ||
return | ||
} | ||
} | ||
|
||
if errors.Is(err, fs.ErrNotExist) { | ||
http.NotFound(w, req) | ||
} else { | ||
InternalError(w, req, log, err) | ||
} | ||
return | ||
} | ||
|
||
response := make([]string, 0) | ||
response = append(response, cfg.Secrets...) | ||
|
||
JsonResult(w, http.StatusOK, response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package api | ||
|
||
// Copyright (C) 2024 by Posit Software, PBC. | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/posit-dev/publisher/internal/config" | ||
"github.com/posit-dev/publisher/internal/logging" | ||
"github.com/posit-dev/publisher/internal/util" | ||
"github.com/posit-dev/publisher/internal/util/utiltest" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type GetConfigSecretsSuite struct { | ||
utiltest.Suite | ||
cwd util.AbsolutePath | ||
log logging.Logger | ||
h http.HandlerFunc | ||
} | ||
|
||
func TestGetConfigSecretsSuite(t *testing.T) { | ||
suite.Run(t, new(GetConfigSecretsSuite)) | ||
} | ||
|
||
func (s *GetConfigSecretsSuite) SetupSuite() { | ||
s.log = logging.New() | ||
} | ||
|
||
func (s *GetConfigSecretsSuite) SetupTest() { | ||
fs := afero.NewMemMapFs() | ||
cwd, err := util.Getwd(fs) | ||
s.Nil(err) | ||
s.cwd = cwd | ||
s.h = GetConfigSecretsHandlerFunc(s.cwd, s.log) | ||
} | ||
|
||
func (s *GetConfigSecretsSuite) TestGetConfigSecrets() { | ||
cfg := config.New() | ||
cfg.Type = config.ContentTypeHTML | ||
cfg.Secrets = []string{ | ||
"secret1", | ||
"secret2", | ||
} | ||
err := cfg.WriteFile(config.GetConfigPath(s.cwd, "myConfig")) | ||
s.NoError(err) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/configurations/myConfig/secrets", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "myConfig"}) | ||
|
||
s.h(rec, req) | ||
|
||
s.Equal(http.StatusOK, rec.Result().StatusCode) | ||
s.Equal("application/json", rec.Header().Get("content-type")) | ||
|
||
res := []string{} | ||
dec := json.NewDecoder(rec.Body) | ||
dec.DisallowUnknownFields() | ||
s.NoError(dec.Decode(&res)) | ||
s.NotNil(res) | ||
s.Equal(cfg.Secrets, res) | ||
} | ||
|
||
func (s *GetConfigSecretsSuite) TestGetConfigSecretsEmptySecrets() { | ||
cfg := config.New() | ||
cfg.Type = config.ContentTypeHTML | ||
err := cfg.WriteFile(config.GetConfigPath(s.cwd, "myConfig")) | ||
s.NoError(err) | ||
|
||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/configurations/myConfig/secrets", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "myConfig"}) | ||
|
||
s.h(rec, req) | ||
|
||
s.Equal(http.StatusOK, rec.Result().StatusCode) | ||
s.Equal("application/json", rec.Header().Get("content-type")) | ||
|
||
res := []string{} | ||
dec := json.NewDecoder(rec.Body) | ||
dec.DisallowUnknownFields() | ||
s.NoError(dec.Decode(&res)) | ||
s.NotNil(res) | ||
s.Equal([]string{}, res) | ||
} | ||
|
||
func (s *GetConfigSecretsSuite) TestGetConfigSecretsNotFound() { | ||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/api/configurations/myConfig/secrets", nil) | ||
s.NoError(err) | ||
req = mux.SetURLVars(req, map[string]string{"name": "myConfig"}) | ||
|
||
s.h(rec, req) | ||
|
||
s.Equal(http.StatusNotFound, rec.Result().StatusCode) | ||
} |