-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef8ebd0
commit e737988
Showing
18 changed files
with
647 additions
and
205 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
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,89 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
if [ -z "$1" ]; then | ||
echo "extension file path not provided" | ||
exit 1 | ||
else | ||
extension_file_path=$1 | ||
fi | ||
|
||
if [ -z "${DEPLOYMENT_NAME}" ]; then | ||
echo "DEPLOYMENT_NAME is not set" | ||
exit 1 | ||
fi | ||
|
||
export SYSTEM_DOMAIN="autoscaler.app-runtime-interfaces.ci.cloudfoundry.org" | ||
export POSTGRES_ADDRESS="${DEPLOYMENT_NAME}-postgres.tcp.${SYSTEM_DOMAIN}" | ||
export STOREPROCEDURE_DB_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/database_password --quiet)" | ||
export POSTGRES_EXTERNAL_PORT="${PR_NUMBER:-5432}" | ||
|
||
export METRICSFORWARDER_HEALTH_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/autoscaler_metricsforwarder_health_password --quiet)" | ||
export METRICSFORWARDER_APPNAME="${METRICSFORWARDER_APPNAME:-"${DEPLOYMENT_NAME}-metricsforwarder"}" | ||
|
||
export POLICY_DB_PASSWORD="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/database_password --quiet)" | ||
export POLICY_DB_SERVER_CA="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/postgres_server --key ca --quiet )" | ||
export POLICY_DB_CLIENT_CERT="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/postgres_server --key certificate --quiet)" | ||
export POLICY_DB_CLIENT_KEY="$(credhub get -n /bosh-autoscaler/${DEPLOYMENT_NAME}/postgres_server --key private_key --quiet)" | ||
|
||
|
||
export STOREPROCEDURE_DB_PASSWORD="${POLICY_DB_PASSWORD}" | ||
export STOREPROCEDURE_DB_SERVER_CA="${POLICY_DB_SERVER_CA}" | ||
export STOREPROCEDURE_DB_CLIENT_CERT="${POLICY_DB_CLIENT_CERT}" | ||
export STOREPROCEDURE_DB_CLIENT_KEY="${POLICY_DB_CLIENT_KEY}" | ||
|
||
|
||
if [ -z "${PR_NUMBER}" ]; then | ||
echo "PR_NUMBER is not set" | ||
exit 1 | ||
fi | ||
|
||
|
||
if [ -z "${SYSTEM_DOMAIN}" ]; then | ||
echo "SYSTEM_DOMAIN is not set" | ||
exit 1 | ||
fi | ||
|
||
if [ -z "${POSTGRES_ADDRESS}" ]; then | ||
echo "POSTGRES_ADDRESS is not set" | ||
exit 1 | ||
fi | ||
|
||
cat <<EOF > "${extension_file_path}" | ||
ID: development | ||
extends: com.github.cloudfoundry.app-autoscaler-release | ||
version: 1.0.0 | ||
_schema-version: 3.3.0 | ||
modules: | ||
- name: metricsforwarder | ||
parameters: | ||
routes: | ||
- route: ${METRICSFORWARDER_APPNAME}.\${default-domain} | ||
resources: | ||
- name: config | ||
parameters: | ||
config: | ||
metricsforwarder: | ||
health: | ||
password: "${METRICSFORWARDER_HEALTH_PASSWORD}" | ||
db: | ||
storedprocedure_db: | ||
url: "postgres://postgres:STOREPROCEDURE_DB_PASSWORD@$POSTGRES_ADDRESS:POSTGRES_EXTERNAL_PORT/autoscaler?application_name=metricsforwarder&sslmode=verify-full&sslrootcert=/home/vcap/app/assets/certs/storedprocedure_db/ca.crt&sslcert=/home/vcap/app/assets/certs/storedprocedure_db/crt&sslkey=/home/vcap/app/assets/certs/storedprocedure_db/key" | ||
- name: policydb | ||
parameters: | ||
config: | ||
uri: "postgres://postgres:${POLICY_DB_PASSWORD}@${POSTGRES_ADDRESS}:${POSTGRES_EXTERNAL_PORT}/autoscaler?application_name=metricsforwarder&sslmode=verify-full" | ||
client_cert: "${POLICY_DB_CLIENT_CERT//$'\n'/\\n}" | ||
client_key: "${POLICY_DB_CLIENT_KEY//$'\n'/\\n}" | ||
server_ca: "${POLICY_DB_SERVER_CA//$'\n'/\\n}" | ||
- name: storedproceduredb | ||
parameters: | ||
config: | ||
uri: "postgres://postgres:${STOREPROCEDURE_DB_PASSWORD}@${POSTGRES_ADDRESS}:${POSTGRES_EXTERNAL_PORT}/autoscaler?application_name=metricsforwarder&sslmode=verify-full" | ||
client_cert: "${STOREPROCEDURE_DB_CLIENT_CERT//$'\n'/\\n}" | ||
client_key: "${STOREPROCEDURE_DB_CLIENT_KEY//$'\n'/\\n}" | ||
server_ca: "${STOREPROCEDURE_DB_SERVER_CA//$'\n'/\\n}" | ||
EOF |
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,97 @@ | ||
package configutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/cloudfoundry-community/go-cfenv" | ||
) | ||
|
||
var ErrReadEnvironment = errors.New("failed to read environment variables") | ||
|
||
type VCAPConfigurationReader interface { | ||
ReadDbFromVCAP(dbName string) (string, error) | ||
// ReadConfigFromVCAP(appEnv *cfenv.App, c *Config) error | ||
} | ||
|
||
type VCAPConfiguration struct { | ||
VCAPConfigurationReader | ||
appEnv *cfenv.App | ||
} | ||
|
||
func NewVCAPConfigurationReader() (*VCAPConfiguration, error) { | ||
vcapConfiguration := &VCAPConfiguration{} | ||
appEnv, err := cfenv.Current() | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %w", ErrReadEnvironment, err) | ||
} | ||
|
||
vcapConfiguration.appEnv = appEnv | ||
return vcapConfiguration, nil | ||
} | ||
|
||
func (vc *VCAPConfiguration) ReadDbFromVCAP(dbName string) (string, error) { | ||
var dbURL *url.URL | ||
var err error | ||
|
||
dbServices, err := vc.appEnv.Services.WithTag("relational") | ||
if err != nil { | ||
fmt.Printf("failed to get db service with relational tag: %s\n", err.Error()) | ||
return "", nil | ||
} | ||
|
||
if len(dbServices) != 1 { | ||
return "", fmt.Errorf("failed to get db service with relational tag") | ||
} | ||
|
||
dbService := dbServices[0] | ||
|
||
dbURI, ok := dbService.CredentialString("uri") | ||
if !ok { | ||
return "", fmt.Errorf("failed to get uri from db service") | ||
} | ||
|
||
fmt.Println(dbURI) | ||
dbURL, err = url.Parse(dbURI) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
parameters, err := url.ParseQuery(dbURL.RawQuery) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = materializeConnectionParameter(dbService, ¶meters, "client_cert", "sslcert") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = materializeConnectionParameter(dbService, ¶meters, "client_key", "sslkey") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = materializeConnectionParameter(dbService, ¶meters, "server_ca", "sslrootcert") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dbURL.RawQuery = parameters.Encode() | ||
|
||
return dbURL.String(), nil | ||
} | ||
|
||
func materializeConnectionParameter(dbService cfenv.Service, parameters *url.Values, bindingProperty string, connectionParameter string) error { | ||
if propertyValue, hasProperty := dbService.CredentialString(bindingProperty); hasProperty { | ||
propertyFile := "/tmp/" + bindingProperty + "." + connectionParameter | ||
err := os.WriteFile(propertyFile, []byte(propertyValue), 0600) | ||
if err != nil { | ||
return err | ||
} | ||
parameters.Set(connectionParameter, propertyFile) | ||
} | ||
return nil | ||
} |
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,13 @@ | ||
package configutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfigutil(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Configutil Suite") | ||
} |
Oops, something went wrong.