From 5e4cea0bb384b29d6435b609366f77fd59e682bf Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Mon, 11 Nov 2024 20:35:17 +0100 Subject: [PATCH] Remove fmt.Println and add Zerolog logging system --- examples/grpcclient/main.go | 11 +++++++---- go.mod | 7 +++++-- go.sum | 21 +++++++++++++++++---- grpcapi/accounts.go | 6 +++--- grpcapi/grpcapi.go | 25 ++++++++++++------------- handlers/accounts.go | 16 ++++++++-------- main.go | 20 ++++++++++++-------- oidc-provider/fosite.go | 11 ++++++++++- oidc-provider/oidc-provider.go | 6 +++--- storage/mongodb.go | 5 +++-- storage/postgresql.go | 15 ++++++++------- 11 files changed, 88 insertions(+), 55 deletions(-) diff --git a/examples/grpcclient/main.go b/examples/grpcclient/main.go index aec3b66..ab4a99f 100755 --- a/examples/grpcclient/main.go +++ b/examples/grpcclient/main.go @@ -3,23 +3,26 @@ package main import ( "context" "encoding/json" - "fmt" "io/ioutil" "os" "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "google.golang.org/grpc" ) func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) if len(os.Args) < 2 { - fmt.Println("missing JSON file path") + log.Error().Msg("missing JSON file path") return } conn, err := grpc.Dial("dns:///localhost:8090", grpc.WithInsecure(), grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"round_robin"}`)) if err != nil { - panic(err) + log.Panic().Err(err).Msg("Cannot dial local server") } client := grpcapi.NewMobilityAccountsClient(conn) @@ -46,5 +49,5 @@ func main() { panic(err) } - fmt.Println(string(jsonresponse)) + log.Debug().Str("response", string(jsonresponse)).Msg("JSOn response") } diff --git a/go.mod b/go.mod index ba00ab1..79cede2 100755 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/lib/pq v1.10.2 github.com/mitchellh/mapstructure v1.5.0 github.com/ory/fosite v0.42.2 + github.com/rs/zerolog v1.33.0 github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 github.com/spf13/viper v1.15.0 github.com/stretchr/testify v1.8.2 @@ -27,7 +28,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect - github.com/coreos/go-systemd/v22 v22.3.2 // indirect + github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgraph-io/ristretto v0.0.3 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect @@ -43,6 +44,8 @@ require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/goveralls v0.0.6 // indirect github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect @@ -74,7 +77,7 @@ require ( go.uber.org/zap v1.21.0 // indirect golang.org/x/net v0.6.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.5.0 // indirect + golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.8.0 // indirect golang.org/x/tools v0.6.0 // indirect google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect diff --git a/go.sum b/go.sum index 28044b8..ec27a31 100644 --- a/go.sum +++ b/go.sum @@ -120,8 +120,9 @@ github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmf github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= @@ -761,6 +762,8 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= @@ -768,6 +771,10 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= @@ -806,7 +813,7 @@ github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= -github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w= +github.com/oleiade/reflections v1.0.0/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= github.com/oleiade/reflections v1.0.1 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0HHQM= github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -924,8 +931,11 @@ github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/rubenv/sql-migrate v0.0.0-20190212093014-1007f53448d7/go.mod h1:WS0rl9eEliYI8DPnr3TOwz4439pay+qNgzJoVya/DmY= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -1370,9 +1380,12 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/grpcapi/accounts.go b/grpcapi/accounts.go index e28c188..e37db4e 100755 --- a/grpcapi/accounts.go +++ b/grpcapi/accounts.go @@ -2,9 +2,9 @@ package grpcapi import ( "encoding/json" - "fmt" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" + "github.com/rs/zerolog/log" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/structpb" ) @@ -27,7 +27,7 @@ func (a Account) ToStorageType() storage.Account { for k, d := range a.Data.GetFields() { jsondata, err := protojson.Marshal(d) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") break } var data any @@ -68,7 +68,7 @@ func AccountFromStorageType(account *storage.Account) (*Account, error) { data, err := structpb.NewStruct(d) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } diff --git a/grpcapi/grpcapi.go b/grpcapi/grpcapi.go index a282a73..b6973b0 100755 --- a/grpcapi/grpcapi.go +++ b/grpcapi/grpcapi.go @@ -2,11 +2,10 @@ package grpcapi import ( context "context" - "fmt" - "log" "net" "git.coopgo.io/coopgo-platform/mobility-accounts/handlers" + "github.com/rs/zerolog/log" "github.com/spf13/viper" "google.golang.org/grpc" codes "google.golang.org/grpc/codes" @@ -31,7 +30,7 @@ func (s MobilityAccountsServerImpl) Login(ctx context.Context, req *LoginRequest } response, err := AccountFromStorageType(account) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err) } return &LoginResponse{Account: response}, nil @@ -40,12 +39,12 @@ func (s MobilityAccountsServerImpl) Register(ctx context.Context, req *RegisterR a := req.Account.ToStorageType() account, err := s.handler.Register(a) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.AlreadyExists, "account creation failed : %v", err) } response, err := AccountFromStorageType(account) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err) } return &RegisterResponse{Account: response}, nil @@ -58,7 +57,7 @@ func (s MobilityAccountsServerImpl) UpdateData(ctx context.Context, req *UpdateD } response, err := AccountFromStorageType(account) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err) } return &UpdateDataResponse{Account: response}, nil @@ -78,12 +77,12 @@ func (s MobilityAccountsServerImpl) UpdatePhoneNumber(ctx context.Context, req * func (s MobilityAccountsServerImpl) GetAccount(ctx context.Context, req *GetAccountRequest) (*GetAccountResponse, error) { account, err := s.handler.GetAccount(req.Id) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err) } response, err := AccountFromStorageType(account) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err) } return &GetAccountResponse{Account: response}, nil @@ -91,12 +90,12 @@ func (s MobilityAccountsServerImpl) GetAccount(ctx context.Context, req *GetAcco func (s MobilityAccountsServerImpl) GetAccountUsername(ctx context.Context, req *GetAccountUsernameRequest) (*GetAccountUsernameResponse, error) { account, err := s.handler.GetAccountUsername(req.Username, req.Namespace) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err) } response, err := AccountFromStorageType(account) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err) } return &GetAccountUsernameResponse{Account: response}, nil @@ -147,13 +146,13 @@ func Run(done chan error, cfg *viper.Viper, handler handlers.MobilityAccountsHan dev_env = cfg.GetBool("dev_env") address = ":" + cfg.GetString("services.grpc.port") ) - fmt.Println("-> GRPC server on", address) + log.Info().Str("address", address).Msg("Running gRPC server") server := grpc.NewServer() RegisterMobilityAccountsServer(server, NewMobilityAccountsServer(handler)) l, err := net.Listen("tcp", address) if err != nil { - log.Fatal(err) + log.Fatal().Str("address", address).Err(err).Msg("gRPC server cannot listen") } if dev_env { @@ -161,7 +160,7 @@ func Run(done chan error, cfg *viper.Viper, handler handlers.MobilityAccountsHan } if err := server.Serve(l); err != nil { - fmt.Println("gRPC service ended") + log.Error().Err(err).Msg("gRPC service ended") done <- err } } diff --git a/handlers/accounts.go b/handlers/accounts.go index eef5d70..822f1c7 100755 --- a/handlers/accounts.go +++ b/handlers/accounts.go @@ -2,12 +2,12 @@ package handlers import ( "errors" - "fmt" "strings" "time" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" "github.com/google/uuid" + "github.com/rs/zerolog/log" "github.com/santhosh-tekuri/jsonschema/v5" "golang.org/x/crypto/bcrypt" ) @@ -19,12 +19,12 @@ func (h MobilityAccountsHandler) Login(username string, password string, namespa u := strings.ToLower(username) account, err := h.storage.DB.LocalAuthentication(namespace, &u, nil, nil) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } if err = bcrypt.CompareHashAndPassword([]byte(account.Authentication.Local.Password), []byte(password)); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } @@ -108,7 +108,7 @@ func (h MobilityAccountsHandler) UpdateData(accountid string, datas map[string]a dataschemas := h.config.GetStringMap("data_schemas") for k, v := range datas { if !h.config.GetBool("allow_any_data") && dataschemas[k] == nil { - fmt.Println("data scope not allowed") + log.Error().Msg("data scope not allowed") return nil, errors.New("data scope not allowed") } @@ -116,19 +116,19 @@ func (h MobilityAccountsHandler) UpdateData(accountid string, datas map[string]a s := dataschemas[k].(map[string]string) sch, err := jsonschema.Compile(s["schema"]) if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } if err = sch.Validate(v); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } } account.Data[k] = v } if err = h.storage.DB.UpdateAccount(*account); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return nil, err } @@ -151,7 +151,7 @@ func (h MobilityAccountsHandler) UpdatePhoneNumber(accountid, phone_number strin account.Authentication.Local.PhoneNumberValidation.ValidationCode = verification_code if err = h.storage.DB.UpdateAccount(*account); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return err } diff --git a/main.go b/main.go index b387f79..8884e57 100755 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main import ( - "fmt" + "os" "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" "git.coopgo.io/coopgo-platform/mobility-accounts/handlers" op "git.coopgo.io/coopgo-platform/mobility-accounts/oidc-provider" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) func main() { @@ -22,20 +24,22 @@ func main() { dev_env = cfg.GetBool("dev_env") ) + if dev_env { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + } + storage, err := storage.NewStorage(cfg) if err != nil { - panic(err) + log.Panic().Err(err).Msg("Cannot connect to storage") + return } handler := handlers.NewHandler(cfg, storage) - fmt.Println("Running", service_name, ":") - if dev_env { - fmt.Printf("\033]0;%s\007", service_name) - } - failed := make(chan error) + log.Info().Str("service_name", service_name).Msg("Running service") + if grpc_enable { go grpcapi.Run(failed, cfg, handler) } @@ -46,6 +50,6 @@ func main() { err = <-failed - fmt.Println("Terminating :", err) + log.Fatal().Err(err).Msg("Terminating server") } diff --git a/oidc-provider/fosite.go b/oidc-provider/fosite.go index d538f4d..6e3b90b 100755 --- a/oidc-provider/fosite.go +++ b/oidc-provider/fosite.go @@ -4,7 +4,9 @@ import ( "context" "crypto/rsa" "errors" + "net/url" "reflect" + "strings" "time" "git.coopgo.io/coopgo-platform/mobility-accounts/handlers" @@ -18,7 +20,14 @@ import ( func NewProvider(c OIDCNamespaceConfig, h handlers.MobilityAccountsHandler, s storage.Storage, privateKey *rsa.PrivateKey) fosite.OAuth2Provider { - config := &compose.Config{} + config := &compose.Config{ + RedirectSecureChecker: func(checkUrl *url.URL) bool { + if strings.HasSuffix(checkUrl.Host, "svc.cluster.local") || strings.HasSuffix(checkUrl.Host, "localhost") { + return true + } + return false + }, + } storage := NewOIDCProviderStore(c, h, s.KV) secret := []byte(c.SecretKey) return compose.ComposeAllEnabled(config, storage, secret, privateKey) diff --git a/oidc-provider/oidc-provider.go b/oidc-provider/oidc-provider.go index 79cd6be..5a9f3c3 100755 --- a/oidc-provider/oidc-provider.go +++ b/oidc-provider/oidc-provider.go @@ -3,12 +3,12 @@ package op import ( "crypto/rand" "crypto/rsa" - "fmt" "git.coopgo.io/coopgo-platform/mobility-accounts/handlers" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" "github.com/mitchellh/mapstructure" "github.com/ory/fosite" + "github.com/rs/zerolog/log" "github.com/spf13/viper" ) @@ -86,14 +86,14 @@ func Run(done chan error, cfg *viper.Viper, handler handlers.MobilityAccountsHan address = "0.0.0.0:" + cfg.GetString("services.oidc_provider.port") ) - fmt.Println("-> OIDC provider endpoints on", address) + log.Info().Str("address", address).Msg("Running OIDC provider") s := NewOIDCHandler(handler, storage, cfg) err := NewOIDCServer(s, cfg) if err != nil { - fmt.Println("OIDC server ended") + log.Error().Err(err).Msg("OIDC server ended") } done <- err diff --git a/storage/mongodb.go b/storage/mongodb.go index 70a5896..50bb9d8 100755 --- a/storage/mongodb.go +++ b/storage/mongodb.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/rs/zerolog/log" "github.com/spf13/viper" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -185,7 +186,7 @@ func (s MongoDBStorage) CreateAccount(account Account) error { func (s MongoDBStorage) UpdateAccount(account Account) error { collection := s.Client.Database(s.DbName).Collection(s.Collections["users"]) if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": account.ID}, account); err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return err } @@ -193,6 +194,6 @@ func (s MongoDBStorage) UpdateAccount(account Account) error { } func (s MongoDBStorage) Migrate() error { - fmt.Println("no migration") + log.Error().Msg("no migration") return nil } diff --git a/storage/postgresql.go b/storage/postgresql.go index 9d6aa2e..78cc210 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -13,6 +13,7 @@ import ( "ariga.io/atlas/sql/schema" "github.com/lib/pq" _ "github.com/lib/pq" + "github.com/rs/zerolog/log" "github.com/spf13/viper" ) @@ -38,12 +39,12 @@ func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) { psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, portInt, user, password, dbname, sslmode) db, err := sql.Open("postgres", psqlconn) if err != nil { - fmt.Println("error", err) + log.Error().Err(err).Msg("error opening postgresql connection") return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed") } err = db.Ping() if err != nil { - fmt.Println(err) + log.Error().Err(err).Msg("") return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed") } return PostgresqlStorage{ @@ -129,7 +130,7 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st req += fmt.Sprintf(` AND phone_number = '%s'`, *phone_number) } req += ";" - fmt.Println(req) + account.Authentication.Local = &LocalAuth{} err := psql.DbConnection.QueryRow(req).Scan( &account.ID, @@ -145,22 +146,22 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st } err = json.Unmarshal(metadata, &account.Metadata) if err != nil { - fmt.Println("one") + log.Error().Err(err).Msg("error unmarshalling account metadata") return nil, err } err = json.Unmarshal(data, &account.Data) if err != nil { - fmt.Println("two") + log.Error().Err(err).Msg("error unmarshalling account data") return nil, err } err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation) if err != nil { - fmt.Println("three") + log.Error().Err(err).Msg("error unmarshalling email validation") return nil, err } err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation) if err != nil { - fmt.Println("four") + log.Error().Err(err).Msg("error unmarshalling phone validation") return nil, err } return account, nil