Skip to content

Commit

Permalink
Support existence checks
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Baillie <[email protected]>
  • Loading branch information
martinbaillie committed Mar 21, 2024
1 parent 62c49f3 commit a7eec52
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions github/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (b *backend) pathConfig() *framework.Path {
Description: descBaseURL,
},
},
ExistenceCheck: b.pathConfigExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: withFieldValidator(b.pathConfigWrite),
Expand Down Expand Up @@ -163,3 +164,19 @@ func (b *backend) pathConfigDelete(

return nil, nil
}

// pathConfigExistenceCheck is implemented on this path to avoid breaking user
// backwards compatibility. The CreateOperation will likely be removed in a
// future major version of the plugin.
func (b *backend) pathConfigExistenceCheck(
ctx context.Context,
req *logical.Request,
_ *framework.FieldData,
) (bool, error) {
entry, err := req.Storage.Get(ctx, pathPatternConfig)
if err != nil {
return false, fmt.Errorf("%s: %w", errConfRetrieval, err)
}

return entry != nil && len(entry.Value) > 0, nil
}
18 changes: 18 additions & 0 deletions github/path_permission_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (b *backend) pathPermissionSet() *framework.Path {
Description: descPerms,
},
},
ExistenceCheck: b.pathPermissionSetExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathPermissionSetDelete,
Expand Down Expand Up @@ -279,3 +280,20 @@ func (b *backend) pathPermissionSetListRead(

return logical.ListResponse(permissionsets), nil
}

// pathPermissionSetExistenceCheck is implemented on this path to avoid breaking
// user backwards compatibility. The CreateOperation will likely be removed in a
// future major version of the plugin.
func (b *backend) pathPermissionSetExistenceCheck(
ctx context.Context, req *logical.Request, d *framework.FieldData,
) (bool, error) {
nameRaw := d.Get("name")
name := nameRaw.(string)

ps, err := getPermissionSet(ctx, name, req.Storage)
if err != nil {
return false, err
}

return ps != nil, nil
}
12 changes: 12 additions & 0 deletions github/path_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (b *backend) pathToken() *framework.Path {
Description: descPerms,
},
},
ExistenceCheck: b.pathTokenExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
// As per the issue request in https://git.io/JUhRk, allow Vault
// Reads (i.e. HTTP GET) to also write the GitHub tokens.
Expand Down Expand Up @@ -166,3 +167,14 @@ func (b *backend) pathTokenWrite(
// Perform the token request.
return client.Token(ctx, tokReq)
}

// pathTokenExistenceCheck always returns false to force the Create path. This
// plugin predates the framework's 'ExistenceCheck' features and we wish to
// avoid changing any contracts with the user at this stage. Tokens are created
// regardless of whether the request is a CREATE, UPDATE or even READ (per a
// user's request (https://git.io/JUhRk).
func (b *backend) pathTokenExistenceCheck(
context.Context, *logical.Request, *framework.FieldData,
) (bool, error) {
return false, nil
}
12 changes: 12 additions & 0 deletions github/path_token_permission_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (b *backend) pathTokenPermissionSet() *framework.Path {
Description: "Required. Name of the permission set.",
},
},
ExistenceCheck: b.pathTokenPermissionSetExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
// As per the issue request in https://git.io/JUhRk, allow Vault
// Reads (i.e. HTTP GET) to also write the GitHub tokens.
Expand Down Expand Up @@ -116,3 +117,14 @@ func (b *backend) pathTokenPermissionSetWrite(
// Perform the token request.
return client.Token(ctx, opts)
}

// pathTokenPermissionSetExistenceCheck always returns false to force the Create
// path. This plugin predates the framework's 'ExistenceCheck' features and we
// wish to avoid changing any contracts with the user at this stage. Tokens are
// created regardless of whether the request is a CREATE, UPDATE or even READ
// (per a user's request (https://git.io/JUhRk).
func (b *backend) pathTokenPermissionSetExistenceCheck(
context.Context, *logical.Request, *framework.FieldData,
) (bool, error) {
return false, nil
}

0 comments on commit a7eec52

Please sign in to comment.