Skip to content
Closed
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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
ROCKETCHAT_SERVER_URL=https://chat.yourcompany.com
ROCKETCHAT_BOT_USERNAME=geekbot
# Wrap password in single quotes if it contains special characters:
# ROCKETCHAT_BOT_PASSWORD='p@ss(w0rd)!'
ROCKETCHAT_BOT_PASSWORD=your-password

# Alternative: read password from a file (avoids shell/env escaping issues)
# Alternative: read password from a file (avoids all escaping issues entirely)
# ROCKETCHAT_BOT_PASSWORD_FILE=/run/secrets/bot_password

ROCKETCHAT_MAIN_ADMIN=admin_username
Expand Down
13 changes: 13 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ type Config struct {
MainAdmin string
}

func trimQuotes(s string) string {
if len(s) < 2 {
return s
}
if (s[0] == '\'' && s[len(s)-1] == '\'') ||
(s[0] == '"' && s[len(s)-1] == '"') {
return s[1 : len(s)-1]
}
return s
}

func Load() (*Config, error) {
botPass := os.Getenv("ROCKETCHAT_BOT_PASSWORD")
botPass = trimQuotes(botPass)
if botPass == "" {
if path := os.Getenv("ROCKETCHAT_BOT_PASSWORD_FILE"); path != "" {
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading ROCKETCHAT_BOT_PASSWORD_FILE: %w", err)
}
botPass = strings.TrimSpace(string(b))
botPass = trimQuotes(botPass)
}
}

Expand Down
30 changes: 30 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"testing"
)

func TestTrimQuotes(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"'password'", "password"},
{`"password"`, "password"},
{`'p@ss(w0rd)!'`, "p@ss(w0rd)!"},
{`"p@ss(w0rd)!"`, "p@ss(w0rd)!"},
{"noquotes", "noquotes"},
{"", ""},
{"'", "'"},
{`"`, `"`},
{"''", ""},
{`""`, ""},
{"a", "a"},
}
for _, tt := range tests {
got := trimQuotes(tt.input)
if got != tt.expected {
t.Errorf("trimQuotes(%q) = %q, want %q", tt.input, got, tt.expected)
}
}
}
Loading