Skip to content
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

Separate secrets from main config #8 #114

Merged
merged 16 commits into from
Sep 30, 2024
15 changes: 13 additions & 2 deletions .github/workflows/blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: build-deploy
on:
workflow_dispatch:
push:
branches: ["master"]
branches: ["feat/master"]
DaniilGo marked this conversation as resolved.
Show resolved Hide resolved
pull_request:
branches: ["master"]
jobs:
Expand Down Expand Up @@ -95,6 +95,17 @@ jobs:
script: |
mkdir -p /var/www/csr/stage/
echo '${{ secrets.DEPLOY_CONFIG }}' > /var/www/csr/stage/config.json
sudo rm -rf /etc/systemd/system/stage.csr.service.d
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think EnvironmentFile directive might be simplier to work with compared to folder management.
https://www.flatcar.org/docs/latest/setup/systemd/environment-variables/
Could you try this approach and see how it works?
Please create environment file in the same directory with stage.csr.service - do not create unnecessary folders as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice! Updated this flow. Here are successful run: https://github.com/CSR-LC/csr-be/actions/runs/11105969158

sudo mkdir -p /etc/systemd/system/stage.csr.service.d
sudo chown root:root /etc/systemd/system/stage.csr.service.d
sudo chmod 755 /etc/systemd/system/stage.csr.service.d
echo -e "[Service]\n\
Environment=\"JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}\"\n\
Environment=\"EMAIL_PASSWORD=${{ secrets.EMAIL_PASSWORD }}\"\n\
Environment=\"DB_USER=${{ secrets.DB_USER }}\"" > override.conf
sudo mv override.conf /etc/systemd/system/stage.csr.service.d/override.conf
sudo chown root:root /etc/systemd/system/stage.csr.service.d/override.conf
sudo chmod 644 /etc/systemd/system/stage.csr.service.d/override.conf
sudo systemctl daemon-reload && sudo service stage.csr stop
cp ~/csr /var/www/csr/stage/server
sudo service stage.csr start
sudo service stage.csr start
3 changes: 0 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
"db": {
"host": "postgres",
"port": "5432",
"user": "csr",
"database": "csr",
"showSql": false
},
"JWTSecretKey": 123,
"email": {
"serverHost": "any",
"serverPort": 1,
"password": "any",
"senderFromAddress": "any",
"senderFromName": "any",
"confirmLinkExpiration": "15m",
Expand Down
13 changes: 12 additions & 1 deletion internal/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func GetAppConfig(additionalDirectories ...string) (*AppConfig, error) {
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read in config: %w", err)
}
bindEnvVars()

conf := getDefaultConfig()
if err := viper.Unmarshal(&conf); err != nil {
Expand All @@ -100,16 +101,18 @@ func GetAppConfig(additionalDirectories ...string) (*AppConfig, error) {

func getDefaultConfig() *AppConfig {
return &AppConfig{
JWTSecretKey: "default_value",
DB: DB{
Host: "localhost",
User: "csr",
Password: "password",
Database: "stage_csr",
},
Password: Password{
Length: 8,
ResetLinkExpiration: 15 * time.Minute,
},
Email: Email{
Password: "default_value",
SenderWebsiteUrl: "https://csr.golangforall.com/",
ConfirmLinkExpiration: 15 * time.Minute,
},
Expand All @@ -119,3 +122,11 @@ func getDefaultConfig() *AppConfig {
},
}
}

func bindEnvVars() {
viper.BindEnv("jwtsecretkey", "JWT_SECRET_KEY")
viper.BindEnv("email.password", "EMAIL_PASSWORD")
viper.BindEnv("db.user", "DB_USER")

viper.AutomaticEnv()
}
Loading