|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/vault/sdk/framework" |
| 10 | + "github.com/hashicorp/vault/sdk/logical" |
| 11 | + "github.com/mitchellh/mapstructure" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + PathConfigFlags = "flags" |
| 16 | +) |
| 17 | + |
| 18 | +var FieldSchemaFlags = map[string]*framework.FieldSchema{ |
| 19 | + "show_config_token": { |
| 20 | + Type: framework.TypeBool, |
| 21 | + Description: "Should we display the token value for the roles?", |
| 22 | + Default: false, |
| 23 | + DisplayAttrs: &framework.DisplayAttributes{Name: "Show Config Token"}, |
| 24 | + }, |
| 25 | +} |
| 26 | + |
| 27 | +func (b *Backend) pathFlagsRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (lResp *logical.Response, err error) { |
| 28 | + b.lockFlagsMutex.RLock() |
| 29 | + defer b.lockFlagsMutex.RUnlock() |
| 30 | + var flagData map[string]any |
| 31 | + err = mapstructure.Decode(b.flags, &flagData) |
| 32 | + return &logical.Response{Data: flagData}, err |
| 33 | +} |
| 34 | + |
| 35 | +func (b *Backend) pathFlagsUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (lResp *logical.Response, err error) { |
| 36 | + b.lockFlagsMutex.Lock() |
| 37 | + defer b.lockFlagsMutex.Unlock() |
| 38 | + |
| 39 | + var eventData = make(map[string]string) |
| 40 | + |
| 41 | + if showConfigToken, ok := data.GetOk("show_config_token"); ok { |
| 42 | + b.flags.ShowConfigToken = showConfigToken.(bool) |
| 43 | + eventData["show_config_token"] = strconv.FormatBool(b.flags.ShowConfigToken) |
| 44 | + } |
| 45 | + |
| 46 | + event(ctx, b.Backend, "flags-write", eventData) |
| 47 | + |
| 48 | + var flagData map[string]any |
| 49 | + err = mapstructure.Decode(b.flags, &flagData) |
| 50 | + return &logical.Response{Data: flagData}, err |
| 51 | +} |
| 52 | + |
| 53 | +func pathFlags(b *Backend) *framework.Path { |
| 54 | + var operations = map[logical.Operation]framework.OperationHandler{ |
| 55 | + logical.ReadOperation: &framework.PathOperation{ |
| 56 | + Callback: b.pathFlagsRead, |
| 57 | + DisplayAttrs: &framework.DisplayAttributes{ |
| 58 | + OperationVerb: "read", |
| 59 | + OperationSuffix: "flags", |
| 60 | + }, |
| 61 | + Summary: "Read the flags for the plugin.", |
| 62 | + Responses: map[int][]framework.Response{ |
| 63 | + http.StatusOK: {{ |
| 64 | + Description: http.StatusText(http.StatusOK), |
| 65 | + Fields: FieldSchemaFlags, |
| 66 | + }}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + if b.flags.AllowRuntimeFlagsChange { |
| 72 | + operations[logical.UpdateOperation] = &framework.PathOperation{ |
| 73 | + Callback: b.pathFlagsUpdate, |
| 74 | + DisplayAttrs: &framework.DisplayAttributes{ |
| 75 | + OperationVerb: "update", |
| 76 | + OperationSuffix: "flags", |
| 77 | + }, |
| 78 | + Summary: "Update the flags for the plugin.", |
| 79 | + Responses: map[int][]framework.Response{ |
| 80 | + http.StatusOK: {{ |
| 81 | + Description: http.StatusText(http.StatusOK), |
| 82 | + Fields: FieldSchemaFlags, |
| 83 | + }}, |
| 84 | + http.StatusBadRequest: {{ |
| 85 | + Description: http.StatusText(http.StatusBadRequest), |
| 86 | + Fields: FieldSchemaFlags, |
| 87 | + }}, |
| 88 | + }, |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return &framework.Path{ |
| 93 | + HelpSynopsis: strings.TrimSpace(pathFlagsHelpSynopsis), |
| 94 | + HelpDescription: strings.TrimSpace(pathFlagsHelpDescription), |
| 95 | + Pattern: PathConfigFlags, |
| 96 | + Fields: FieldSchemaFlags, |
| 97 | + DisplayAttrs: &framework.DisplayAttributes{ |
| 98 | + OperationPrefix: operationPrefixGitlabAccessTokens, |
| 99 | + }, |
| 100 | + Operations: operations, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const pathFlagsHelpSynopsis = `Flags for the plugin.` |
| 105 | + |
| 106 | +const pathFlagsHelpDescription = `` |
0 commit comments