-
Notifications
You must be signed in to change notification settings - Fork 1
do config update #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
do config update #36
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -376,6 +376,117 @@ func (c *Client) DoUpdate(ctx context.Context, creds keys.Credentials) ([]byte, | |
| return result.Config, nebulaPrivkeyPEM, newCreds, meta, nil | ||
| } | ||
|
|
||
| // DoConfigUpdate sends a signed message to the DNClient API to fetch the new configuration update. During this call new keys | ||
| // are generated for DNClient API communication. If the API response is successful, the new configuration | ||
| // is returned along with the new DNClient API credentials and a meta object. | ||
| // | ||
| // See dnapi.InsertConfigPrivateKey and dnapi.InsertConfigCert for how to insert the old Nebula cert/private key into the configuration. | ||
| func (c *Client) DoConfigUpdate(ctx context.Context, creds keys.Credentials) ([]byte, *keys.Credentials, *ConfigMeta, error) { | ||
| // Rotate key | ||
| var hostPrivkey keys.PrivateKey // ECDSA | ||
|
|
||
| newKeys, err := keys.New() | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to generate new keys: %s", err) | ||
| } | ||
|
|
||
| msg := message.DoConfigUpdateRequest{ | ||
| Nonce: nonce(), | ||
| } | ||
|
|
||
| // Set the correct keypair based on the current private key type | ||
| switch creds.PrivateKey.Unwrap().(type) { | ||
| case ed25519.PrivateKey: | ||
| hostPubkeyPEM, err := newKeys.HostEd25519PublicKey.MarshalPEM() | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to marshal Ed25519 public key: %s", err) | ||
| } | ||
| hostPrivkey = newKeys.HostEd25519PrivateKey | ||
| msg.HostPubkeyEd25519 = hostPubkeyPEM | ||
| case *ecdsa.PrivateKey: | ||
| hostPubkeyPEM, err := newKeys.HostP256PublicKey.MarshalPEM() | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to marshal P256 public key: %s", err) | ||
| } | ||
| hostPrivkey = newKeys.HostP256PrivateKey | ||
| msg.HostPubkeyP256 = hostPubkeyPEM | ||
| } | ||
|
|
||
| blob, err := json.Marshal(msg) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to marshal DNClient message: %s", err) | ||
| } | ||
|
|
||
| // Make API call | ||
| resp, err := c.postDNClient(ctx, message.DoConfigUpdate, blob, creds.HostID, creds.Counter, creds.PrivateKey) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to make API call to Defined Networking: %w", err) | ||
| } | ||
| resultWrapper := message.SignedResponseWrapper{} | ||
| err = json.Unmarshal(resp, &resultWrapper) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to unmarshal signed response wrapper: %s", err) | ||
| } | ||
|
|
||
| // Verify the signature | ||
| valid := false | ||
| for _, caPubkey := range creds.TrustedKeys { | ||
| if caPubkey.Verify(resultWrapper.Data.Message, resultWrapper.Data.Signature) { | ||
| valid = true | ||
| break | ||
| } | ||
| } | ||
| if !valid { | ||
| return nil, nil, nil, fmt.Errorf("failed to verify signed API result") | ||
| } | ||
|
Member
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. is it possible to DRY the signature validation so we only have one copy to maintain? just since it's crypto bits... like lines 425 - 441
Contributor
Author
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. PR updated! |
||
|
|
||
| // Consume the verified message | ||
| result := message.DoConfigUpdateResponse{} | ||
| err = json.Unmarshal(resultWrapper.Data.Message, &result) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to unmarshal response (%s): %s", resultWrapper.Data.Message, err) | ||
| } | ||
|
|
||
| // Verify the nonce | ||
| if !bytes.Equal(result.Nonce, msg.Nonce) { | ||
| return nil, nil, nil, fmt.Errorf("nonce mismatch between request (%s) and response (%s)", msg.Nonce, result.Nonce) | ||
| } | ||
|
|
||
| // Verify the counter | ||
| if result.Counter <= creds.Counter { | ||
| return nil, nil, nil, fmt.Errorf("counter in request (%d) should be less than counter in response (%d)", creds.Counter, result.Counter) | ||
| } | ||
|
|
||
| trustedKeys, err := keys.TrustedKeysFromPEM(result.TrustedKeys) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("failed to load trusted keys from bundle: %s", err) | ||
| } | ||
|
|
||
| newCreds := &keys.Credentials{ | ||
| HostID: creds.HostID, | ||
| Counter: result.Counter, | ||
| PrivateKey: hostPrivkey, | ||
| TrustedKeys: trustedKeys, | ||
| } | ||
|
|
||
| meta := &ConfigMeta{ | ||
| Org: ConfigOrg{ | ||
| ID: result.Organization.ID, | ||
| Name: result.Organization.Name, | ||
| }, | ||
| Network: ConfigNetwork{ | ||
| ID: result.Network.ID, | ||
| Name: result.Network.Name, | ||
| }, | ||
| Host: ConfigHost{ | ||
| ID: result.Host.ID, | ||
| Name: result.Host.Name, | ||
| IPAddress: result.Host.IPAddress, | ||
| }, | ||
| } | ||
|
|
||
| return result.Config, newCreds, meta, nil | ||
| } | ||
| func (c *Client) CommandResponse(ctx context.Context, creds keys.Credentials, responseToken string, response any) error { | ||
| value, err := json.Marshal(message.CommandResponseRequest{ | ||
| ResponseToken: responseToken, | ||
|
|
||
This file contains hidden or 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 hidden or 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.
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.
nit: comment name is off
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.
donezo