From 9f163e3638aa5cbe754f9308ff5754c50fb4b5ea Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:52:25 +0200 Subject: [PATCH] checking if error is not server closed and closing the client after operations are done --- .gitignore | 1 + cmd/root.go | 6 ++++-- hack/rootCA.pem | 22 ---------------------- pkg/bitwarden/bitwarden.go | 7 ++++--- pkg/server/server.go | 11 +++++------ 5 files changed, 14 insertions(+), 33 deletions(-) delete mode 100644 hack/rootCA.pem diff --git a/.gitignore b/.gitignore index d723441..4d5df68 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work.sum bin/ dist/ hack/cert-manager.yaml +hack/rootCA.pem diff --git a/cmd/root.go b/cmd/root.go index ce7b85f..32a18ec 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,9 @@ package cmd import ( "context" + "errors" "log/slog" + "net/http" "os" "os/signal" "syscall" @@ -54,8 +56,8 @@ const timeout = 15 * time.Second func runServeCmd(_ *cobra.Command, _ []string) error { svr := server.NewServer(rootArgs.server) go func() { - if err := svr.Run(context.Background()); err != nil { - slog.Error("server stopped", "error", err) + if err := svr.Run(context.Background()); err != nil && !errors.Is(err, http.ErrServerClosed) { + slog.Error("server stopped unexpectedly", "error", err) } }() diff --git a/hack/rootCA.pem b/hack/rootCA.pem deleted file mode 100644 index 1d43d53..0000000 --- a/hack/rootCA.pem +++ /dev/null @@ -1,22 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIDszCCApugAwIBAgIRAMGiIBGuNGKQ6WoBh4Pgh8gwDQYJKoZIhvcNAQELBQAw -QzEcMBoGA1UEChMTZXh0ZXJuYWwtc2VjcmV0cy5pbzEjMCEGA1UEAxMaY2VydC1t -YW5hZ2VyLWJpdHdhcmRlbi10bHMwHhcNMjQwNjE3MTUyNzQ2WhcNMjQwOTE1MTUy -NzQ2WjBDMRwwGgYDVQQKExNleHRlcm5hbC1zZWNyZXRzLmlvMS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/pkg/bitwarden/bitwarden.go b/pkg/bitwarden/bitwarden.go index 9a7322b..d975370 100644 --- a/pkg/bitwarden/bitwarden.go +++ b/pkg/bitwarden/bitwarden.go @@ -17,6 +17,7 @@ package bitwarden import ( "context" "fmt" + "log/slog" "net/http" "github.com/bitwarden/sdk-go" @@ -74,12 +75,12 @@ func Login(req *LoginRequest) (sdk.BitwardenClientInterface, error) { identityURL := setOrDefault(req.IdentityURL, defaultIdentityURL) statePath := setOrDefault(req.StatePath, defaultStatePath) - // TODO: Cache the client... or the session? + // Client is closed in the calling handlers. + slog.Debug("constructed client with api and identity url", "api", apiURL, "identityUrl", identityURL, "statePath", statePath) bitwardenClient, err := sdk.NewBitwardenClient(&apiURL, &identityURL) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create client: %w", err) } - defer bitwardenClient.Close() if err := bitwardenClient.AccessTokenLogin(req.AccessToken, &statePath); err != nil { return nil, fmt.Errorf("bitwarden login: %w", err) diff --git a/pkg/server/server.go b/pkg/server/server.go index 6f9b1f3..7205e08 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -97,10 +97,11 @@ func (s *Server) getSecretHandler(w http.ResponseWriter, r *http.Request) { return } + defer c.Close() secretResponse, err := c.Secrets().Get(request.ID) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, "failed to get secret: "+err.Error(), http.StatusBadRequest) return } @@ -108,10 +109,6 @@ func (s *Server) getSecretHandler(w http.ResponseWriter, r *http.Request) { s.handleResponse(secretResponse, w) } -type DeleteSecretRequest struct { - SecretIDs []string `json:"secretIds"` -} - func (s *Server) deleteSecretHandler(w http.ResponseWriter, r *http.Request) { request := &sdk.SecretsDeleteRequest{} c, err := s.getClient(r, &request) @@ -120,6 +117,7 @@ func (s *Server) deleteSecretHandler(w http.ResponseWriter, r *http.Request) { return } + defer c.Close() response, err := c.Secrets().Delete(request.IDS) if err != nil { @@ -139,10 +137,11 @@ func (s *Server) createSecretHandler(w http.ResponseWriter, r *http.Request) { return } + defer c.Close() response, err := c.Secrets().Create(request.Key, request.Value, request.Note, request.OrganizationID, request.ProjectIDS) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, "failed to create secret: "+err.Error(), http.StatusBadRequest) return }