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

main: load secrets from disk if CREDENTIALS_DIRECTORY is set #36

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ func main() {
ws.fsqOauthConfig.ClientSecret = v
}

if v, ok := os.LookupEnv("CREDENTIALS_DIRECTORY"); ok {
l.Printf("loading credentials from files in directory %s", v)

if s, err := os.ReadFile(filepath.Join(v, "secure-key")); err == nil {
secureKeyFlag = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "ot-publish-password")); err == nil {
otPassword = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "auth-client-secret")); err == nil {
ah.ClientSecret = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "fsq-client-id")); err == nil {
ws.fsqOauthConfig.ClientID = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "fsq-client-secret")); err == nil {
ws.fsqOauthConfig.ClientSecret = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "tripit-api-key")); err == nil {
ws.tripitAPIKey = strings.TrimSpace(string(s))
tpsync.oauthAPIKey = strings.TrimSpace(string(s))
}
if s, err := os.ReadFile(filepath.Join(v, "tripit-api-secret")); err == nil {
ws.tripitAPISecret = strings.TrimSpace(string(s))
tpsync.oauthAPISecret = strings.TrimSpace(string(s))
}
}

var errs []string

if secureKeyFlag == "" {
Expand All @@ -152,7 +180,7 @@ func main() {
if ah.Issuer == "" {
errs = append(errs, "auth-client-id required")
}
if ah.Issuer == "" {
if ah.ClientSecret == "" {
errs = append(errs, "auth-client-secret required")
}
if ah.Issuer == "" {
Expand Down Expand Up @@ -265,6 +293,13 @@ func main() {
}

if !disableTripitSync {
if v := os.Getenv("TRIPIT_API_KEY"); v != "" && ws.tripitAPIKey == "" {
ws.tripitAPIKey = v
}
if v := os.Getenv("TRIPIT_API_SECRET"); v != "" && ws.tripitAPISecret == "" {
ws.tripitAPISecret = v
}

if ws.tripitAPIKey == "" || ws.tripitAPISecret == "" {
l.Fatal("tripit oauth1 config not set on ws")
}
Expand Down Expand Up @@ -302,7 +337,6 @@ func main() {
}, func(error) {
tripitSyncDone <- struct{}{}
log.Print("returning tripit shutdown")

})

}
Expand All @@ -325,7 +359,6 @@ func main() {
l.Printf("shutting down main http server: %v", err)
}
log.Print("returning http shutdown")

})

if promListen != "" {
Expand Down Expand Up @@ -572,7 +605,7 @@ func (s *secretsManager) Save() error {
if err != nil {
return fmt.Errorf("marshaling secrets: %s", err)
}
if err := os.WriteFile(s.path, b, 0600); err != nil {
if err := os.WriteFile(s.path, b, 0o600); err != nil {
return fmt.Errorf("writing secrets to %s: %v", s.path, err)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions main_tripitimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type tripitSyncCommand struct {
}

func (t *tripitSyncCommand) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&t.oauthAPIKey, "tripit-api-key", getEnvDefault("TRIPIT_API_KEY", ""), "Oauth1 API Key for Tripit")
fs.StringVar(&t.oauthAPISecret, "tripit-api-secret", getEnvDefault("TRIPIT_API_SECRET", ""), "Oauth1 API Secret for Tripit")
fs.StringVar(&t.oauthAPIKey, "tripit-api-key", "", "Oauth1 API Key for Tripit")
fs.StringVar(&t.oauthAPISecret, "tripit-api-secret", "", "Oauth1 API Secret for Tripit")
}

func (t *tripitSyncCommand) Validate() error {
Expand Down
Loading