-
Notifications
You must be signed in to change notification settings - Fork 540
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
performance improvements on mount fetch #2152
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
29289a1
add mountutil and GetMount
fairclothjm e089e42
fix nomad backend existence check
fairclothjm 598e841
fix context arg
fairclothjm ab08a93
use GET instead of LIST for auth mount fetching
fairclothjm 5b3ce04
normalize error response
fairclothjm be01d35
changelog
fairclothjm 997b786
fix github auth path
fairclothjm 524375a
fix auth existence checking
fairclothjm 3fcf3cc
use contexts and add log
fairclothjm d3ac54b
fix build and rebase
fairclothjm 777bd47
make golangci-lint happy
fairclothjm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mountutil | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-provider-vault/internal/consts" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
// Error strings that are returned by the Vault API. | ||
const ( | ||
ErrVaultSecretMountNotFound = "No secret engine mount at" | ||
ErrVaultAuthMountNotFound = "No auth engine at" | ||
) | ||
|
||
// Error strings that are used internally by TFVP | ||
var ( | ||
// ErrMountNotFound is used to signal to resources that a secret or auth | ||
// mount does not exist and should be removed from state. | ||
ErrMountNotFound = errors.New("mount not found") | ||
) | ||
|
||
// GetMount will fetch the secret mount at the given path. | ||
func GetMount(ctx context.Context, client *api.Client, path string) (*api.MountOutput, error) { | ||
mount, err := client.Sys().GetMountWithContext(ctx, path) | ||
// Hardcoding the error string check is not ideal, but Vault does not | ||
// return 404 in this case | ||
if err != nil && strings.Contains(err.Error(), ErrVaultSecretMountNotFound) || mount == nil { | ||
return nil, fmt.Errorf("%w: %s", ErrMountNotFound, err) | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading from Vault: %s", err) | ||
} | ||
return mount, nil | ||
} | ||
|
||
// GetAuthMount will fetch the auth mount at the given path. | ||
func GetAuthMount(ctx context.Context, client *api.Client, path string) (*api.MountOutput, error) { | ||
mount, err := client.Sys().GetAuthWithContext(ctx, path) | ||
// Hardcoding the error string check is not ideal, but Vault does not | ||
// return 404 in this case | ||
if err != nil && strings.Contains(err.Error(), ErrVaultAuthMountNotFound) || mount == nil { | ||
return nil, fmt.Errorf("%w: %s", ErrMountNotFound, err) | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading from Vault: %s", err) | ||
} | ||
return mount, nil | ||
} | ||
|
||
// NormalizeMountPath to be in a form valid for accessing values from api.MountOutput | ||
func NormalizeMountPath(path string) string { | ||
return TrimSlashes(path) + consts.PathDelim | ||
} | ||
|
||
// TrimSlashes from path. | ||
func TrimSlashes(path string) string { | ||
return strings.Trim(path, consts.PathDelim) | ||
} | ||
|
||
// CheckMountEnabledWithContext in Vault | ||
func CheckMountEnabledWithContext(ctx context.Context, client *api.Client, path string) (bool, error) { | ||
_, err := GetMount(ctx, client, path) | ||
if errors.Is(err, ErrMountNotFound) { | ||
return false, err | ||
} | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// CheckMountEnabled in Vault | ||
func CheckMountEnabled(client *api.Client, path string) (bool, error) { | ||
return CheckMountEnabledWithContext(context.Background(), client, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we want the error checks for d.Set to be ignored, it seems like we can set up an exclusion: https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml#L266-L299
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.
Thanks! I think we should probably enforce error checking in this case just to be safe.