Skip to content

Commit

Permalink
Fix new hide metadata contribution
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Baillie <[email protected]>
  • Loading branch information
martinbaillie committed Mar 20, 2024
1 parent 9adcf63 commit 4704d2b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
7 changes: 3 additions & 4 deletions github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,10 @@ func (c *Client) token(ctx context.Context, tokReq *tokenRequest) (*logical.Resp
return nil, fmt.Errorf("%w: %v", errUnableToDecodeAccessTokenRes, err)
}

// If repository metadata is disabled, reduce memory consumption by trimming
// If excluding repository metadata, reduce memory consumption by trimming
// it to a simple list of repository names.
if !c.IncludeRepositoryMetadata {
repositoriesKey, included := resData["repositories"]
if included {
if c.ExcludeRepositoryMetadata {
if repositoriesKey, ok := resData["repositories"]; ok {
var (
repos = repositoriesKey.([]any)
repoNames = make([]any, 0, len(repos))
Expand Down
22 changes: 12 additions & 10 deletions github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const (

// Config holds all configuration for the backend.
type Config struct {
// AppID is the application identifier of the GitHub App.
AppID int `json:"app_id"`

// PrvKey is the private for signing GitHub access token requests (JWTs).
// NOTE: Should be in a PEM PKCS#1 RSAPrivateKey format.
PrvKey string `json:"prv_key"`
Expand All @@ -32,12 +29,17 @@ type Config struct {
// Defaults to GitHub's public API.
BaseURL string `json:"base_url"`

// IncludeRepositoryMetadata controls filtering of the 'repositories' key returned on repository-filtered tokens
// Defaults to returning full repository metadata; returns a minimised list of repository names if set to false
IncludeRepositoryMetadata bool `json:"include_repository_metadata"`
// AppID is the application identifier of the GitHub App.
AppID int `json:"app_id"`

// ExcludeRepositoryMetadata controls filtering of the 'repositories' key
// returned on repository-filtered tokens. It defaults to returning full
// repository metadata but will return a minimised list of repository names
// if set.
ExcludeRepositoryMetadata bool `json:"exclude_repository_metadata"`
}

// NewConfig returns a pre-configured Config struct.
// NewConfig returns a pre-configured Config struct with defaults.
func NewConfig() *Config {
return &Config{BaseURL: githubPublicAPI}
}
Expand Down Expand Up @@ -83,9 +85,9 @@ func (c *Config) Update(d *framework.FieldData) (bool, error) {
}
}

if includeRepositoryMetadata, ok := d.GetOk(includeRepositoryMetadataKey); ok {
if nv := includeRepositoryMetadata.(bool); c.IncludeRepositoryMetadata != nv {
c.IncludeRepositoryMetadata = nv
if irm, ok := d.GetOk(keyExcludeRepositoryMetadata); ok {
if nv := irm.(bool); c.ExcludeRepositoryMetadata != nv {
c.ExcludeRepositoryMetadata = nv
changed = true
}
}
Expand Down
5 changes: 2 additions & 3 deletions github/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func TestConfig_Update(t *testing.T) {
t.Parallel()

cases := []struct {
name string
err error
new *Config
exp *Config
data *framework.FieldData
name string
changed bool
err error
}{
{
name: "Empty",
Expand Down Expand Up @@ -148,7 +148,6 @@ func TestConfig_Update(t *testing.T) {
}

for _, tc := range cases {

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down
25 changes: 12 additions & 13 deletions github/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ const (
)

const (
keyAppID = "app_id"
descAppID = "Application ID of the GitHub App."
keyPrvKey = "prv_key"
includeRepositoryMetadataKey = "include_repository_metadata"

descPrvKey = "Private key for signing GitHub access token requests (JWTs)."
descIncludeRepositoryMetadataKey = "If set to true, the token lease response 'data.repositories' sub-field will be minimized to 'data.repositories.*.names'"
keyBaseURL = "base_url"
descBaseURL = "Base URL for API requests (defaults to the public GitHub API)."
keyAppID = "app_id"
descAppID = "Application ID of the GitHub App."
keyPrvKey = "prv_key"
descPrvKey = "Private key for signing GitHub access token requests (JWTs)."
keyBaseURL = "base_url"
descBaseURL = "Base URL for API requests (defaults to the public GitHub API)."
keyExcludeRepositoryMetadata = "exclude_repository_metadata"
descExcludeRepositoryMetadata = "Minimise token response 'data.repositories' content to 'data.repositories.*.names'"
)

const pathConfigHelpSyn = `
Expand All @@ -54,11 +53,11 @@ func (b *backend) pathConfig() *framework.Path {
Description: descPrvKey,
Required: true,
},
includeRepositoryMetadataKey: {
keyExcludeRepositoryMetadata: {
Type: framework.TypeBool,
Description: descIncludeRepositoryMetadataKey,
Description: descExcludeRepositoryMetadata,
Required: false,
Default: true,
Default: false,
},
keyBaseURL: {
Type: framework.TypeString,
Expand Down Expand Up @@ -99,7 +98,7 @@ func (b *backend) pathConfigRead(
resData := map[string]any{
keyAppID: c.AppID,
keyBaseURL: c.BaseURL,
includeRepositoryMetadataKey: c.IncludeRepositoryMetadata,
keyExcludeRepositoryMetadata: c.ExcludeRepositoryMetadata,
}

// We don't return the key but indicate its presence for a better UX.
Expand Down

0 comments on commit 4704d2b

Please sign in to comment.