Skip to content

Commit

Permalink
Providing Kong admin token via file Kong#4789
Browse files Browse the repository at this point in the history
Add tests and checks.
  • Loading branch information
ludovic-pourrat committed Oct 11, 2023
1 parent 2f59bf1 commit 9115470
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/manager/config_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (c *Config) Validate() error {
return fmt.Errorf("can't set both --kong-admin-svc and --kong-admin-url")
}
}
if c.KongAdminToken != "" && c.KongAdminTokenPath != "" {
return errors.New("both admin token and admin token file specified, only one allowed")
}

if err := c.validateKonnect(); err != nil {
return fmt.Errorf("invalid konnect configuration: %w", err)
Expand Down
44 changes: 44 additions & 0 deletions internal/manager/config_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager_test
import (
"bytes"
"fmt"
"os"
"testing"

"github.com/samber/mo"
Expand Down Expand Up @@ -335,6 +336,49 @@ func TestConfigValidate(t *testing.T) {
require.NoError(t, c.Validate())
})
})

t.Run("Admin Token", func(t *testing.T) {
validWithToken := func() manager.Config {
return manager.Config{
KongAdminToken: "non-empty-token",
}
}

t.Run("admin token accepted", func(t *testing.T) {
c := validWithToken()
require.NoError(t, c.Validate())
require.NoError(t, c.Resolve())
})
})

t.Run("Admin Token Path", func(t *testing.T) {
validWithTokenPath := func() manager.Config {
tempDir := t.TempDir()
tokenFile, err := os.CreateTemp(tempDir, "kong.token")
require.NoError(t, err)
_, err = tokenFile.Write([]byte("non-empty-token"))
require.NoError(t, err)
return manager.Config{
KongAdminTokenPath: tokenFile.Name()

Check failure on line 362 in internal/manager/config_validation_test.go

View workflow job for this annotation

GitHub Actions / Run Go benchmarks

missing ',' before newline in composite literal
,
}
}

t.Run("admin token path accepted", func(t *testing.T) {
c := validWithTokenPath()
require.NoError(t, c.Validate())
require.NoError(t, c.Resolve())
require.Equal(t, c.KongAdminToken, "non-empty-token")
})

t.Run("admin token and token path rejected", func(t *testing.T) {
c := validWithTokenPath()
c.KongAdminToken = "non-empty-token"
require.ErrorContains(t, c.Validate(), "both admin token and admin token file specified, only one allowed")
})

})

}

func TestConfigValidateGatewayDiscovery(t *testing.T) {
Expand Down

0 comments on commit 9115470

Please sign in to comment.