-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from trisacrypto/sc-22232
Store password endpoint
- Loading branch information
Showing
11 changed files
with
333 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
|
||
docker build -t trisa/courier:latest -f ./containers/Dockerfile . | ||
docker build -t trisa/courier:latest -f ./containers/courier/Dockerfile . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package api_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/trisacrypto/courier/pkg/api/v1" | ||
) | ||
|
||
func TestStoreCertificatePassword(t *testing.T) { | ||
// Create a test server | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, http.MethodPost, r.Method) | ||
require.Equal(t, "/v1/certs/1234/pkcs12password", r.URL.Path) | ||
w.WriteHeader(http.StatusNoContent) | ||
})) | ||
defer ts.Close() | ||
|
||
// Create a client to test the client method | ||
client, err := api.New(ts.URL) | ||
require.NoError(t, err, "could not create client") | ||
|
||
// Create a new register request | ||
req := &api.StorePasswordRequest{ | ||
ID: "1234", | ||
Password: "hunter2", | ||
} | ||
err = client.StoreCertificatePassword(context.Background(), req) | ||
require.NoError(t, err, "could not execute password store request") | ||
|
||
// Should error if there is no ID in the request | ||
req.ID = "" | ||
err = client.StoreCertificatePassword(context.Background(), req) | ||
require.ErrorIs(t, err, api.ErrIDRequired, "client should error if no ID is provided") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package courier | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/trisacrypto/courier/pkg/api/v1" | ||
) | ||
|
||
// StoreCertificatePassword stores the password for an encrypted certificate and | ||
// returns a 204 No Content response. | ||
func (s *Server) StoreCertificatePassword(c *gin.Context) { | ||
var ( | ||
err error | ||
req *api.StorePasswordRequest | ||
) | ||
|
||
// Parse the request body | ||
req = &api.StorePasswordRequest{} | ||
if err := c.BindJSON(req); err != nil { | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse(err)) | ||
return | ||
} | ||
|
||
// Password is required | ||
if req.Password == "" { | ||
c.JSON(http.StatusBadRequest, api.ErrorResponse("missing password in request")) | ||
return | ||
} | ||
|
||
// Store the password | ||
if err = s.store.UpdatePassword(c.Param("id"), []byte(req.Password)); err != nil { | ||
c.JSON(http.StatusInternalServerError, api.ErrorResponse(err)) | ||
return | ||
} | ||
|
||
// Return 204 No Content | ||
c.Status(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package courier_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/trisacrypto/courier/pkg/api/v1" | ||
) | ||
|
||
func (s *courierTestSuite) TestStoreCertificatePassword() { | ||
require := s.Require() | ||
|
||
s.Run("HappyPath", func() { | ||
// Configure the store mock to return a successful response | ||
req := &api.StorePasswordRequest{ | ||
ID: "certID", | ||
Password: "password", | ||
} | ||
s.store.OnUpdatePassword = func(name string, password []byte) error { | ||
require.Equal(req.ID, name, "wrong password name passed to store") | ||
require.Equal([]byte(req.Password), password, "wrong password passed to store") | ||
return nil | ||
} | ||
defer s.store.Reset() | ||
|
||
// Make a request to the endpoint | ||
err := s.client.StoreCertificatePassword(context.Background(), req) | ||
require.NoError(err, "could not store certificate password") | ||
}) | ||
|
||
s.Run("MissingPassword", func() { | ||
req := &api.StorePasswordRequest{ | ||
ID: "certID", | ||
} | ||
err := s.client.StoreCertificatePassword(context.Background(), req) | ||
s.CheckHTTPStatus(err, http.StatusBadRequest, "wrong error code for missing password") | ||
}) | ||
|
||
s.Run("StoreError", func() { | ||
s.store.OnUpdatePassword = func(name string, password []byte) error { | ||
return errors.New("internal store error") | ||
} | ||
defer s.store.Reset() | ||
|
||
req := &api.StorePasswordRequest{ | ||
ID: "certID", | ||
Password: "password", | ||
} | ||
err := s.client.StoreCertificatePassword(context.Background(), req) | ||
s.CheckHTTPStatus(err, http.StatusInternalServerError, "wrong error code for store error") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.