From fb3d0296c41302f9af9966912d81f72c451000d4 Mon Sep 17 00:00:00 2001 From: Martin Baillie Date: Wed, 20 Mar 2024 20:46:33 +1100 Subject: [PATCH] Fix new hide metadata contribution Signed-off-by: Martin Baillie --- github/client.go | 7 +++---- github/config.go | 22 ++++++++++++---------- github/config_test.go | 5 ++--- github/path_config.go | 25 ++++++++++++------------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/github/client.go b/github/client.go index 485db93..36f1b40 100644 --- a/github/client.go +++ b/github/client.go @@ -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)) diff --git a/github/config.go b/github/config.go index 6f05ad8..b48ba03 100644 --- a/github/config.go +++ b/github/config.go @@ -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"` @@ -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} } @@ -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 } } diff --git a/github/config_test.go b/github/config_test.go index 1943634..f1948dc 100644 --- a/github/config_test.go +++ b/github/config_test.go @@ -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", @@ -148,7 +148,6 @@ func TestConfig_Update(t *testing.T) { } for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/github/path_config.go b/github/path_config.go index 4b5d3cd..4c85ab8 100644 --- a/github/path_config.go +++ b/github/path_config.go @@ -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 = ` @@ -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, @@ -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.