-
Notifications
You must be signed in to change notification settings - Fork 46
feat: support custom headers #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f5174c7
6f9b420
59399fb
540a9ea
80b85f9
0eba131
d9497fb
3d4df97
181be80
a1f2afe
ea77157
1890217
d05304d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [tools] | ||
| go = "1.26.2" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,12 +31,30 @@ func BindViperToFlags(cmd *cobra.Command, viperInstance *viper.Viper) { | |
|
|
||
| if !flag.Changed && viperInstance.IsSet(configName) { | ||
| value := viperInstance.Get(configName) | ||
| err := cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", value)) | ||
| cobra.CheckErr(err) | ||
| for _, strVal := range viperValueToStrings(value) { | ||
| cobra.CheckErr(cmd.Flags().Set(flag.Name, strVal)) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| for _, subcmd := range cmd.Commands() { | ||
| BindViperToFlags(subcmd, viperInstance) | ||
| } | ||
| } | ||
|
|
||
| // viperValueToStrings converts a Viper config value to a slice of strings | ||
| // suitable for pflag.Set calls. Slice values (from YAML lists) produce one | ||
| // string per element; scalar values produce a single-element slice. | ||
| func viperValueToStrings(value any) []string { | ||
| sliceValue, ok := value.([]any) | ||
| if !ok { | ||
| return []string{fmt.Sprintf("%v", value)} | ||
| } | ||
|
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be false for every kind of slice that is not |
||
|
|
||
| result := make([]string, 0, len(sliceValue)) | ||
| for _, elem := range sliceValue { | ||
| result = append(result, fmt.Sprintf("%v", elem)) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| Copyright © 2023 OpenFGA | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package cmdutils | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestViperValueToStrings(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testcases := []struct { | ||
| name string | ||
| value any | ||
| expected []string | ||
| }{ | ||
| { | ||
| name: "slice value produces one string per element", | ||
| value: []any{ | ||
| "X-Custom-Header: value1", | ||
| "X-Request-ID: abc123", | ||
| }, | ||
| expected: []string{"X-Custom-Header: value1", "X-Request-ID: abc123"}, | ||
| }, | ||
| { | ||
| name: "single element slice", | ||
| value: []any{"X-Custom-Header: value1"}, | ||
| expected: []string{"X-Custom-Header: value1"}, | ||
| }, | ||
| { | ||
| name: "empty slice", | ||
| value: []any{}, | ||
| expected: []string{}, | ||
| }, | ||
| { | ||
| name: "scalar string produces single-element slice", | ||
| value: "https://api.fga.example", | ||
| expected: []string{"https://api.fga.example"}, | ||
| }, | ||
| { | ||
| name: "boolean value is stringified", | ||
| value: true, | ||
| expected: []string{"true"}, | ||
| }, | ||
| { | ||
| name: "integer value is stringified", | ||
| value: 42, | ||
| expected: []string{"42"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range testcases { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| result := viperValueToStrings(test.value) | ||
| assert.Equal(t, test.expected, result) | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ limitations under the License. | |
| package fga | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| openfga "github.com/openfga/go-sdk" | ||
|
|
@@ -32,23 +34,34 @@ const ( | |
| MinSdkWaitInMs = 500 | ||
| ) | ||
|
|
||
| var userAgent = "openfga-cli/" + build.Version | ||
| var ( | ||
| userAgent = "openfga-cli/" + build.Version | ||
|
|
||
| ErrInvalidHeaderFormat = errors.New("expected format \"Header-Name: value\"") | ||
| ErrEmptyHeaderName = errors.New("header name must not be empty") | ||
| ) | ||
|
|
||
| type ClientConfig struct { | ||
| ApiUrl string `json:"api_url,omitempty"` //nolint:revive,stylecheck | ||
| StoreID string `json:"store_id,omitempty"` | ||
| AuthorizationModelID string `json:"authorization_model_id,omitempty"` | ||
| APIToken string `json:"api_token,omitempty"` //nolint:gosec | ||
| APIToken string `json:"api_token,omitempty"` | ||
| APITokenIssuer string `json:"api_token_issuer,omitempty"` | ||
| APIAudience string `json:"api_audience,omitempty"` | ||
| APIScopes []string `json:"api_scopes,omitempty"` | ||
| ClientID string `json:"client_id,omitempty"` | ||
| ClientSecret string `json:"client_secret,omitempty"` //nolint:gosec | ||
| ClientSecret string `json:"client_secret,omitempty"` | ||
| CustomHeaders []string `json:"custom_headers,omitempty"` | ||
| Debug bool `json:"debug,omitempty"` | ||
| } | ||
|
|
||
| func (c ClientConfig) GetFgaClient() (*client.OpenFgaClient, error) { | ||
| fgaClient, err := client.NewSdkClient(c.getClientConfig()) | ||
| clientConfig, err := c.getClientConfig() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| fgaClient, err := client.NewSdkClient(clientConfig) | ||
| if err != nil { | ||
| return nil, err //nolint:wrapcheck | ||
| } | ||
|
|
@@ -84,7 +97,12 @@ func (c ClientConfig) getCredentials() *credentials.Credentials { | |
| } | ||
| } | ||
|
|
||
| func (c ClientConfig) getClientConfig() *client.ClientConfiguration { | ||
| func (c ClientConfig) getClientConfig() (*client.ClientConfiguration, error) { | ||
| customHeaders, err := c.getCustomHeaders() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid custom headers configuration: %w", err) | ||
| } | ||
|
|
||
| return &client.ClientConfiguration{ | ||
| ApiUrl: c.ApiUrl, | ||
| StoreId: c.StoreID, | ||
|
|
@@ -95,6 +113,27 @@ func (c ClientConfig) getClientConfig() *client.ClientConfiguration { | |
| MaxRetry: MaxSdkRetry, | ||
| MinWaitInMs: MinSdkWaitInMs, | ||
| }, | ||
| Debug: c.Debug, | ||
| Debug: c.Debug, | ||
| DefaultHeaders: customHeaders, | ||
| }, nil | ||
| } | ||
|
|
||
| func (c ClientConfig) getCustomHeaders() (map[string]string, error) { | ||
| headers := map[string]string{} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prevent unnecessary allocations: header := make(map[string]string, len(c.CustomHeaders)) |
||
|
|
||
| for _, header := range c.CustomHeaders { | ||
| parts := strings.SplitN(header, ":", 2) | ||
| if len(parts) != 2 { | ||
| return nil, fmt.Errorf("invalid custom header %q: %w", header, ErrInvalidHeaderFormat) | ||
| } | ||
|
alexanderzobnin marked this conversation as resolved.
Comment on lines
+125
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use name, value, _ := strings.Cut(header, ":")
name, value = strings.TrimSpace(name), strings.TrimSpace(value)
if name == "" {
return nil, fmt.Errorf("...")
}
headers[name] = value |
||
|
|
||
| name := strings.TrimSpace(parts[0]) | ||
| if name == "" { | ||
| return nil, fmt.Errorf("invalid custom header %q: %w", header, ErrEmptyHeaderName) | ||
| } | ||
|
|
||
| headers[name] = strings.TrimSpace(parts[1]) | ||
| } | ||
|
|
||
| return headers, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
???