This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Sign config files in cli #149
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -8,7 +8,6 @@ package createconfigcmd | |
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
@@ -18,8 +17,8 @@ import ( | |
"strings" | ||
|
||
docdid "github.com/hyperledger/aries-framework-go/pkg/doc/did" | ||
"github.com/hyperledger/aries-framework-go/pkg/doc/jose" | ||
"github.com/spf13/cobra" | ||
gojose "github.com/square/go-jose/v3" | ||
cmdutils "github.com/trustbloc/edge-core/pkg/utils/cmd" | ||
tlsutils "github.com/trustbloc/edge-core/pkg/utils/tls" | ||
|
||
|
@@ -79,10 +78,11 @@ type memberData struct { | |
Policy models.StakeholderSettings `json:"policy"` | ||
// Endpoints is a list of sidetree endpoints owned by this stakeholder organization | ||
Endpoints []string `json:"endpoints"` | ||
// PrivateKeyJwk is privatekey jwk | ||
PrivateKeyJwk json.RawMessage `json:"privateKeyJwk,omitempty"` | ||
// PrivateKeyJwk is privatekey jwk file | ||
PrivateKeyJwkPath string `json:"privateKeyJwkPath,omitempty"` | ||
|
||
jsonWebKey jose.JWK | ||
jsonWebKey gojose.JSONWebKey | ||
sigKey gojose.SigningKey | ||
} | ||
|
||
type didClient interface { | ||
|
@@ -156,8 +156,10 @@ func createCreateConfigCmd() *cobra.Command { | |
} | ||
|
||
func writeConfig(outputDirectory string, filesData map[string][]byte) error { | ||
if err := os.MkdirAll(outputDirectory, 0700); err != nil { | ||
return err | ||
if outputDirectory != "" { | ||
if err := os.MkdirAll(outputDirectory, 0700); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for k, v := range filesData { | ||
|
@@ -189,9 +191,16 @@ func getConfig(cmd *cobra.Command) (*config, error) { | |
} | ||
|
||
for _, member := range config.MembersData { | ||
if err := member.jsonWebKey.UnmarshalJSON(member.PrivateKeyJwk); err != nil { | ||
return nil, err | ||
jwkData, err := ioutil.ReadFile(member.PrivateKeyJwkPath) //nolint: gosec | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i hope we are not assuming all keys are available on a single system. some stakeholders will sign their own copy on their own system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i.e. this is somewhat like fabric endorsement - generally ask the stakeholder to sign it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
return nil, fmt.Errorf("failed to read jwk file '%s' : %w", member.PrivateKeyJwkPath, err) | ||
} | ||
|
||
if err := member.jsonWebKey.UnmarshalJSON(jwkData); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal to jwk: %w", err) | ||
} | ||
// TODO add support for ECDSA using P-256 and SHA-256 | ||
member.sigKey = gojose.SigningKey{Key: member.jsonWebKey.Key, Algorithm: gojose.EdDSA} | ||
} | ||
|
||
return &config, nil | ||
|
@@ -233,6 +242,7 @@ func createFlags(startCmd *cobra.Command) { | |
|
||
func createConfig(parameters *parameters) (map[string][]byte, error) { | ||
filesData := make(map[string][]byte) | ||
sigKeys := make([]gojose.SigningKey, 0) | ||
|
||
consortium := models.Consortium{Domain: parameters.config.ConsortiumData.Domain, | ||
Policy: parameters.config.ConsortiumData.Policy} | ||
|
@@ -260,40 +270,46 @@ func createConfig(parameters *parameters) (map[string][]byte, error) { | |
return nil, err | ||
} | ||
|
||
jwsBytes, err := signConfig(stakeholderBytes) | ||
jws, err := signConfig(stakeholderBytes, []gojose.SigningKey{member.sigKey}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filesData[member.Domain] = jwsBytes | ||
sigKeys = append(sigKeys, member.sigKey) | ||
|
||
filesData[member.Domain] = []byte(jws) | ||
} | ||
|
||
consortiumBytes, err := json.Marshal(consortium) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jwsBytes, err := signConfig(consortiumBytes) | ||
jws, err := signConfig(consortiumBytes, sigKeys) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filesData[consortium.Domain] = jwsBytes | ||
filesData[consortium.Domain] = []byte(jws) | ||
|
||
return filesData, nil | ||
} | ||
|
||
func signConfig(configBytes []byte) ([]byte, error) { | ||
// TODO add logic for jws | ||
// for now return dummy jws | ||
// remove this code after adding logic for jws | ||
m := make(map[string]interface{}) | ||
m["payload"] = base64.RawURLEncoding.EncodeToString(configBytes) | ||
func signConfig(configBytes []byte, keys []gojose.SigningKey) (string, error) { | ||
signer, err := gojose.NewMultiSigner(keys, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
jws, err := signer.Sign(configBytes) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return json.Marshal(m) | ||
return jws.FullSerialize(), nil | ||
} | ||
|
||
func createDID(didClient didClient, sidetreeURL string, jwk *jose.JWK) (*docdid.Doc, error) { | ||
func createDID(didClient didClient, sidetreeURL string, jwk *gojose.JSONWebKey) (*docdid.Doc, error) { | ||
pubKey, err := jwk.Public().MarshalJSON() | ||
if err != nil { | ||
return nil, err | ||
|
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
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,7 @@ | ||
{ | ||
"kty": "OKP", | ||
"kid": "key1", | ||
"d": "CSLczqR1ly2lpyBcWne9gFKnsjaKJw0dKfoSQu7lNvg", | ||
"crv": "Ed25519", | ||
"x": "bWRCy8DtNhRO3HdKTFB2eEG5Ac1J00D0DQPffOwtAD0" | ||
} |
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,7 @@ | ||
{ | ||
"kty": "OKP", | ||
"kid": "key1", | ||
"d": "-YawjZSeB9Rkdol9SHeOcT9hIvo_VuH6zM-pgtk3b10", | ||
"crv": "Ed25519", | ||
"x": "8rfXFZNHZs9GYzGbQLYDasGUAm1brAgTLI0jrD4KheU" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will need some additional followup.
a) the private key might not be in a file.
b) the private key can be protected.
c) might make sense to refactor how keys are associated to stakeholders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do that as followup