From 3bc17d6aba35f484eb04c9a55b86baf145d59cff Mon Sep 17 00:00:00 2001 From: chapeau Date: Sun, 22 Oct 2023 13:22:54 +0200 Subject: [PATCH] feat #35: Add sqlite3 storage backend --- Dockerfile | 2 +- polyculeconnect/go.mod | 3 ++ polyculeconnect/go.sum | 6 ++++ polyculeconnect/main.go | 61 +++++++++++++++++++++++++++++++---------- 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 58c51c9..cb9aa2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ ARG TARGETPLATFORM ARG BUILDPLATFORM WORKDIR /go/src/git.faercol.me/polyculeconnect COPY polyculeconnect ./ -RUN CGO_ENABLED=0 make build +RUN make build # Replace with from scratch later on FROM --platform=$TARGETPLATFORM alpine:latest diff --git a/polyculeconnect/go.mod b/polyculeconnect/go.mod index 8c7a0f7..4c28fbd 100644 --- a/polyculeconnect/go.mod +++ b/polyculeconnect/go.mod @@ -27,6 +27,7 @@ require ( github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect github.com/go-jose/go-jose/v3 v3.0.0 // indirect github.com/go-ldap/ldap/v3 v3.4.6 // indirect + github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/s2a-go v0.1.7 // indirect @@ -38,7 +39,9 @@ require ( github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.11 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect + github.com/lib/pq v1.10.9 // indirect github.com/mattermost/xml-roundtrip-validator v0.1.0 // indirect + github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/reflectwalk v1.0.0 // indirect diff --git a/polyculeconnect/go.sum b/polyculeconnect/go.sum index 4b8f5f0..320f43a 100644 --- a/polyculeconnect/go.sum +++ b/polyculeconnect/go.sum @@ -51,6 +51,8 @@ github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyM github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= github.com/go-ldap/ldap/v3 v3.4.6 h1:ert95MdbiG7aWo/oPYp9btL3KJlMPKnP58r09rI8T+A= github.com/go-ldap/ldap/v3 v3.4.6/go.mod h1:IGMQANNtxpsOzj7uUAMjpGBaOVTC4DYyIy8VsTdxmtc= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= @@ -106,8 +108,12 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattermost/xml-roundtrip-validator v0.1.0 h1:RXbVD2UAl7A7nOTR4u7E3ILa4IbtvKBHw64LDsmu9hU= github.com/mattermost/xml-roundtrip-validator v0.1.0/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To= +github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= diff --git a/polyculeconnect/main.go b/polyculeconnect/main.go index 145a0ab..cf1ee12 100644 --- a/polyculeconnect/main.go +++ b/polyculeconnect/main.go @@ -13,9 +13,11 @@ import ( "git.faercol.me/faercol/polyculeconnect/polyculeconnect/connector" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/server" + "github.com/dexidp/dex/connector/oidc" dex_server "github.com/dexidp/dex/server" "github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage/memory" + "github.com/dexidp/dex/storage/sql" "github.com/prometheus/client_golang/prometheus" ) @@ -37,15 +39,43 @@ func parseArgs() *cliArgs { func initStorage(conf *config.AppConfig) (storage.Storage, error) { var storageType storage.Storage + var err error switch conf.StorageType { case "memory": storageType = memory.New(logger.L) + case "sqlite": + sqlconfig := sql.SQLite3{ + File: conf.StorageConfig.File, + } + storageType, err = sqlconfig.Open(logger.L) + if err != nil { + logger.L.Fatalf("Failed to initialize sqlite backend: %s", err.Error()) + } default: return storageType, fmt.Errorf("unsupported storage backend type: %s", conf.StorageType) } return storageType, nil } +func createConnector(backend *config.BackendConfig, dexConf *dex_server.Config, connectorIDs []string) error { + for _, id := range connectorIDs { + if id == backend.ID { + return nil + } + } + + backendConfJson, err := json.Marshal(backend.Config) + if err != nil { + return fmt.Errorf("failed to serialize oidc config for backend %q: %s", backend.Name, err.Error()) + } + return dexConf.Storage.CreateConnector(storage.Connector{ + ID: backend.ID, + Name: backend.Name, + Type: string(backend.Type), + Config: backendConfJson, + }) +} + func main() { args := parseArgs() @@ -81,27 +111,28 @@ func main() { logger.L.Info("Initializing authentication backends") dex_server.ConnectorsConfig["refuseAll"] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) } - if err := dexConf.Storage.CreateConnector(storage.Connector{ - ID: "null", + connectors, err := dexConf.Storage.ListConnectors() + if err != nil { + logger.L.Fatalf("Failed to get existing connectors: %s", err.Error()) + } + var connectorIDs []string + for _, conn := range connectors { + connectorIDs = append(connectorIDs, conn.ID) + } + + backend := config.BackendConfig{ + Config: &oidc.Config{}, Name: "RefuseAll", + ID: "null", Type: "refuseAll", - Config: nil, - }); err != nil { + } + + if err := createConnector(&backend, &dexConf, connectorIDs); err != nil { logger.L.Errorf("Failed to add connector for backend RefuseAll to stage: %s", err.Error()) } for _, backend := range conf.OpenConnectConfig.BackendConfigs { - backendConfJson, err := json.Marshal(backend.Config) - if err != nil { - logger.L.Errorf("Failed to serialize oidc config for backend %q: %s", backend.Name, err.Error()) - continue - } - if err := dexConf.Storage.CreateConnector(storage.Connector{ - ID: backend.ID, - Name: backend.Name, - Type: string(backend.Type), - Config: backendConfJson, - }); err != nil { + if err := createConnector(backend, &dexConf, connectorIDs); err != nil { logger.L.Errorf("Failed to add connector for backend %q to stage: %s", backend.Name, err.Error()) continue }