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

feat: Support environment variable configuration #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cli
import (
"errors"
"fmt"
"os"
"strings"

"github.com/micromdm/nanomdm/storage"
Expand Down Expand Up @@ -38,7 +39,21 @@ func NewStorage() *Storage {
return &Storage{}
}

func fallbackAccumulator(s *StringAccumulator, envVarFallback string) {
if len(*s) > 0 {
return
}

if envValue := os.Getenv(envVarFallback); envValue != "" {
s.Set(envValue)
}
}

func (s *Storage) Parse(logger log.Logger) (storage.AllStorage, error) {
fallbackAccumulator(&s.Storage, "STORAGE")
fallbackAccumulator(&s.DSN, "STORAGE_DSN")
fallbackAccumulator(&s.Options, "STORAGE_OPTIONS")

if len(s.Storage) != len(s.DSN) {
return nil, errors.New("must have same number of storage and DSN flags")
}
Expand Down
46 changes: 30 additions & 16 deletions cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ func main() {
flag.Var(&cliStorage.DSN, "dsn", "data source name; deprecated: use -storage-dsn")
flag.Var(&cliStorage.Options, "storage-options", "storage backend options")
var (
flListen = flag.String("listen", ":9000", "HTTP listen address")
flAPIKey = flag.String("api", "", "API key for API endpoints")
flVersion = flag.Bool("version", false, "print version")
flRootsPath = flag.String("ca", "", "path to PEM CA cert(s)")
flIntsPath = flag.String("intermediate", "", "path to PEM intermediate cert(s)")
flWebhook = flag.String("webhook-url", "", "URL to send requests to")
flCertHeader = flag.String("cert-header", "", "HTTP header containing URL-escaped TLS client certificate")
flDebug = flag.Bool("debug", false, "log debug messages")
flDump = flag.Bool("dump", false, "dump MDM requests and responses to stdout")
flDisableMDM = flag.Bool("disable-mdm", false, "disable MDM HTTP endpoint")
flCheckin = flag.Bool("checkin", false, "enable separate HTTP endpoint for MDM check-ins")
flMigration = flag.Bool("migration", false, "HTTP endpoint for enrollment migrations")
flRetro = flag.Bool("retro", false, "Allow retroactive certificate-authorization association")
flDMURLPfx = flag.String("dm", "", "URL to send Declarative Management requests to")
flAuthProxy = flag.String("auth-proxy-url", "", "Reverse proxy URL target for MDM-authenticated HTTP requests")
flUAZLChal = flag.Bool("ua-zl-dc", false, "reply with zero-length DigestChallenge for UserAuthenticate")
flListen = flag.String("listen", envString("LISTEN", ":9000"), "HTTP listen address")
flAPIKey = flag.String("api", envString("API", ""), "API key for API endpoints")
flVersion = flag.Bool("version", envBool("VERSION"), "print version")
flRootsPath = flag.String("ca", envString("CA", ""), "path to PEM CA cert(s)")
flIntsPath = flag.String("intermediate", envString("INTERMEDIATE", ""), "path to PEM intermediate cert(s)")
flWebhook = flag.String("webhook-url", envString("WEBHOOK_URL", ""), "URL to send requests to")
flCertHeader = flag.String("cert-header", envString("CERT_HEADER", ""), "HTTP header containing URL-escaped TLS client certificate")
flDebug = flag.Bool("debug", envBool("DEBUG"), "log debug messages")
flDump = flag.Bool("dump", envBool("DUMP"), "dump MDM requests and responses to stdout")
flDisableMDM = flag.Bool("disable-mdm", envBool("DISABLE_MDM"), "disable MDM HTTP endpoint")
flCheckin = flag.Bool("checkin", envBool("CHECKIN"), "enable separate HTTP endpoint for MDM check-ins")
flMigration = flag.Bool("migration", envBool("MIGRATION"), "HTTP endpoint for enrollment migrations")
flRetro = flag.Bool("retro", envBool("RETRO"), "Allow retroactive certificate-authorization association")
flDMURLPfx = flag.String("dm", envString("DM", ""), "URL to send Declarative Management requests to")
flAuthProxy = flag.String("auth-proxy-url", envString("AUTH_PROXY_URL", ""), "Reverse proxy URL target for MDM-authenticated HTTP requests")
flUAZLChal = flag.Bool("ua-zl-dc", envBool("UA_ZL_DC"), "reply with zero-length DigestChallenge for UserAuthenticate")
)
flag.Parse()

Expand Down Expand Up @@ -272,3 +272,17 @@ func newTraceID(_ *http.Request) string {
rand.Read(b)
return fmt.Sprintf("%x", b)
}

func envString(key, def string) string {
if env := os.Getenv(key); env != "" {
return env
}
return def
}

func envBool(key string) bool {
if env := os.Getenv(key); env == "true" {
return true
}
return false
}