Skip to content

Commit

Permalink
checking if error is not server closed and closing the client after o…
Browse files Browse the repository at this point in the history
…perations are done
  • Loading branch information
Skarlso committed Jun 17, 2024
1 parent 57234ac commit 9f163e3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ go.work.sum
bin/
dist/
hack/cert-manager.yaml
hack/rootCA.pem
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package cmd

import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -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)
}
}()

Expand Down
22 changes: 0 additions & 22 deletions hack/rootCA.pem

This file was deleted.

7 changes: 4 additions & 3 deletions pkg/bitwarden/bitwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package bitwarden
import (
"context"
"fmt"
"log/slog"
"net/http"

"github.com/bitwarden/sdk-go"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,18 @@ 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
}

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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 9f163e3

Please sign in to comment.