-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from ovotech/add-gcf-entrypoint
Add gcf entrypoint
- Loading branch information
Showing
9 changed files
with
210 additions
and
242 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cloudfunction | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/ovotech/cloud-key-rotator/pkg/config" | ||
"github.com/ovotech/cloud-key-rotator/pkg/log" | ||
"github.com/ovotech/cloud-key-rotator/pkg/rotate" | ||
) | ||
|
||
var logger = log.StdoutLogger().Sugar() | ||
|
||
// Request is the CloudFunction entrypoint | ||
func Request(w http.ResponseWriter, r *http.Request) { | ||
var c config.Config | ||
var err error | ||
var bucketName string | ||
var ok bool | ||
bucketEnvVarName := "CKR_BUCKET_NAME" | ||
if bucketName, ok = os.LookupEnv(bucketEnvVarName); !ok { | ||
logCloudFunctionError(w, fmt.Errorf("Env var: %s is required", bucketEnvVarName)) | ||
return | ||
} | ||
if c, err = config.GetConfigFromGCS( | ||
bucketName, | ||
getEnv("CKR_SECRET_CONFIG_NAME", "ckr-config.json"), | ||
getEnv("CKR_CONFIG_TYPE", "json")); err != nil { | ||
logCloudFunctionError(w, err) | ||
return | ||
} | ||
if err = rotate.Rotate("", "", "", c); err != nil { | ||
logCloudFunctionError(w, err) | ||
return | ||
} | ||
} | ||
|
||
func logCloudFunctionError(w http.ResponseWriter, err error) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
logger.Error(err) | ||
} | ||
|
||
//getEnv returns the value of the env var matching the key, if it exists, and | ||
// the value of fallback otherwise | ||
func getEnv(key, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok { | ||
return value | ||
} | ||
return fallback | ||
} |
File renamed without changes.
File renamed without changes.
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