Skip to content

Commit

Permalink
Merge pull request #21 from XenitAB/static-secret
Browse files Browse the repository at this point in the history
add static secret support
  • Loading branch information
simongottschlag committed Nov 6, 2023
2 parents 6279820 + 6d5755d commit 475ac6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ func run(ctx context.Context, cfg config) error {

_, err = oidcTokenHandler.ParseToken(c.Request.Context(), token)
if err != nil {
//nolint:errcheck // ignore
c.AbortWithError(http.StatusForbidden, fmt.Errorf("unable to validate the token: %v", err))
return
switch {
case cfg.StaticSecret != "" && token == cfg.StaticSecret:
// do nothing, valid static secret
default:
//nolint:errcheck // ignore
c.AbortWithError(http.StatusForbidden, fmt.Errorf("unable to validate the token: %v", err))
return
}
}

rp.ServeHTTP(c.Writer, c.Request)
Expand Down Expand Up @@ -175,6 +180,7 @@ type config struct {
AzureContainerRegistryUser string `json:"azure_container_registry_user" arg:"--azure-container-registry-user,env:AZURE_CONTAINER_REGISTRY_USER,required" help:"The user for the Azure Container Registry that should be proxied."`
AzureContainerRegistryPassword string `json:"azure_container_registry_password" arg:"--azure-container-registry-password,env:AZURE_CONTAINER_REGISTRY_PASSWORD,required" help:"The password for the Azure Container Registry that should be proxied."`
AllowedTenantIDs []string `json:"allowed_tenant_ids" arg:"--allowed-tenant-ids,env:ALLOWED_TENANT_IDS,required" help:"A list of the allowed tenant ids that can use the proxy."`
StaticSecret string `json:"static_secret" arg:"--static-secret,env:STATIC_SECRET" help:"A static secret, that if set, can be used instead of token authentication."`

issuer string
audience string
Expand Down
21 changes: 21 additions & 0 deletions src/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestDefault(t *testing.T) {
AzureContainerRegistryUser: "ze-user",
AzureContainerRegistryPassword: "ze-pass",
AllowedTenantIDs: []string{"ze-tenant-id"},
StaticSecret: "ze-static-secret",
Address: fmt.Sprintf(":%d", port),
}

Expand Down Expand Up @@ -170,6 +171,26 @@ func TestDefault(t *testing.T) {
require.Equal(t, http.StatusForbidden, res.StatusCode)
})

t.Run("GET /v2/foobar valid token", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/v2/foobar", port), http.NoBody)
require.NoError(t, err)
req.SetBasicAuth("will-be-ignored", op.GetToken(t).AccessToken)

res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
})

t.Run("GET /v2/foobar valid static secret", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/v2/foobar", port), http.NoBody)
require.NoError(t, err)
req.SetBasicAuth("will-be-ignored", "ze-static-secret")

res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
})

cancel()
err := g.Wait()
require.NoError(t, err)
Expand Down

0 comments on commit 475ac6b

Please sign in to comment.