-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /check endpoint to verify env variable setup (#18)
Adds a `/check` endpoint that will return a 204 if the required environment variables are present, and otherwise returns a 400 with a list of what variables are missing. Co-authored-by: Theron Voran <tvoran@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 deletions.
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
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,54 @@ | ||
package kubesecrets | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/hashicorp/vault/sdk/framework" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
const ( | ||
checkPath = "check" | ||
checkHelpSynopsis = `Checks the Kubernetes configuration is valid.` | ||
checkHelpDescription = `Checks the Kubernetes configuration is valid, checking if required environment variables are set.` | ||
) | ||
|
||
var envVarsToCheck = []string{k8sServiceHostEnv, k8sServicePortEnv} | ||
|
||
func (b *backend) pathCheck() *framework.Path { | ||
return &framework.Path{ | ||
Pattern: checkPath + "/?$", | ||
Operations: map[logical.Operation]framework.OperationHandler{ | ||
logical.ReadOperation: &framework.PathOperation{ | ||
Callback: b.pathCheckRead, | ||
}, | ||
}, | ||
HelpSynopsis: checkHelpSynopsis, | ||
HelpDescription: checkHelpDescription, | ||
} | ||
} | ||
|
||
func (b *backend) pathCheckRead(_ context.Context, _ *logical.Request, _ *framework.FieldData) (*logical.Response, error) { | ||
var missing []string | ||
for _, key := range envVarsToCheck { | ||
val := os.Getenv(key) | ||
if val == "" { | ||
missing = append(missing, key) | ||
} | ||
} | ||
|
||
if len(missing) == 0 { | ||
return &logical.Response{ | ||
Data: map[string]interface{}{ | ||
logical.HTTPStatusCode: http.StatusNoContent, | ||
}, | ||
}, nil | ||
} | ||
|
||
missingText := strings.Join(missing, ", ") | ||
return logical.ErrorResponse(fmt.Sprintf("Missing environment variables: %s", missingText)), nil | ||
} |