-
Notifications
You must be signed in to change notification settings - Fork 92
feat: add notation certificate cleanup-test command
#1231
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
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b8b795b
initial commit
230c50e
initial commit
4ada922
update
46d50b4
resolve conflicts
97ed01f
fix unit test
fd4bbda
e2e test
d9f97cb
e2e tests
f697214
e2e tests
19aeda7
e2e tests
60697ea
e2e tests
f9c95d8
Merge branch 'notaryproject:main' into clean-test
21f306a
e2e tests
f2c8102
resolve conflicts
cad8120
e2e tests
99e6804
e2e tests
15c6bf1
e2e tests
283359f
e2e tests
c5367b7
e2e tests
a6a224d
e2e tests
5680f18
e2e tests
0e4270a
clean up
d866270
clean up
8dff0ec
update
e29b050
update
2f144b3
Merge branch 'notaryproject:main' into clean-test
0fb53a3
resolve conflicts
7ab3257
update
6bbc9ef
update
ade54ce
update
796337f
update
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package cert | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/notaryproject/notation-go/config" | ||
| "github.com/notaryproject/notation-go/dir" | ||
| "github.com/notaryproject/notation/v2/cmd/notation/internal/display" | ||
| "github.com/notaryproject/notation/v2/cmd/notation/internal/truststore" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type certCleanupTestOpts struct { | ||
| name string | ||
| confirmed bool | ||
| } | ||
|
|
||
| func certCleanupTestCommand(opts *certCleanupTestOpts) *cobra.Command { | ||
| if opts == nil { | ||
| opts = &certCleanupTestOpts{} | ||
| } | ||
| command := &cobra.Command{ | ||
| Use: "cleanup-test [flags] <common_name>", | ||
| Short: `Clean up a test RSA key and its corresponding certificate that were generated using the "generate-test" command.`, | ||
| Args: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) == 0 { | ||
| return errors.New("missing certificate common name") | ||
| } | ||
| if !truststore.IsValidFileName(args[0]) { | ||
| return errors.New("certificate common name must follow [a-zA-Z0-9_.-]+ format") | ||
| } | ||
| opts.name = args[0] | ||
| return nil | ||
| }, | ||
| Long: `Clean up a test RSA key and its corresponding certificate that were generated using the "generate-test" command. | ||
|
|
||
| Example - Clean up a test key and corresponding certificate named "wabbit-networks.io": | ||
| notation cert cleanup-test wabbit-networks.io | ||
| `, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cleanupTestCert(opts) | ||
| }, | ||
| } | ||
| command.Flags().BoolVarP(&opts.confirmed, "yes", "y", false, "do not prompt for confirmation") | ||
| return command | ||
| } | ||
|
|
||
| func cleanupTestCert(opts *certCleanupTestOpts) error { | ||
| name := opts.name | ||
| relativeKeyPath, relativeCertPath := dir.LocalKeyPath(name) | ||
| certPath, _ := dir.ConfigFS().SysPath(relativeCertPath) // err is always nil | ||
| certFileName := filepath.Base(certPath) | ||
| keyPath, _ := dir.ConfigFS().SysPath(relativeKeyPath) // err is always nil | ||
| prompt := fmt.Sprintf(`The test key %s and its corresponding certificate will be cleaned up with the following changes: | ||
| - Delete certificate %s.crt from trust store %s of type ca | ||
| - Remove key %s from the key list | ||
| - Delete key file: %s | ||
| - Delete certificate file: %s | ||
|
|
||
| Are you sure you want to continue?`, name, name, name, name, keyPath, certPath) | ||
| confirmed, err := display.AskForConfirmation(os.Stdin, prompt, opts.confirmed) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !confirmed { | ||
| return nil | ||
| } | ||
|
|
||
| // 1. remove from trust store | ||
| err = truststore.DeleteCert("ca", name, certFileName, true) | ||
| if err != nil { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return fmt.Errorf("failed to delete certificate %s from trust store %s of type ca: %w", certFileName, name, err) | ||
| } | ||
| fmt.Printf("Certificate %s does not exist in trust store %s of type ca\n", certFileName, name) | ||
| } | ||
|
|
||
| // 2. remove key from signingkeys.json config | ||
| exec := func(s *config.SigningKeys) error { | ||
| _, err := s.Remove(name) | ||
| return err | ||
| } | ||
| err = config.LoadExecSaveSigningKeys(exec) | ||
| if err != nil { | ||
| var keyNotFoundError config.KeyNotFoundError | ||
| if !errors.As(err, &keyNotFoundError) { | ||
| return fmt.Errorf("failed to remove key %s from the key list: %w", name, err) | ||
| } | ||
| fmt.Printf("Key %s does not exist in the key list\n", name) | ||
| } else { | ||
| fmt.Printf("Successfully removed key %s from the key list\n", name) | ||
| } | ||
|
|
||
| // 3. delete key and certificate files from LocalKeyPath | ||
| err = os.Remove(keyPath) | ||
| if err != nil { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return fmt.Errorf("failed to delete key file %s: %w", keyPath, err) | ||
| } | ||
| fmt.Printf("Key file %s does not exist\n", keyPath) | ||
| } else { | ||
| fmt.Printf("Successfully deleted key file: %s\n", keyPath) | ||
| } | ||
| err = os.Remove(certPath) | ||
| if err != nil { | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return fmt.Errorf("failed to delete certificate file %s: %w", certPath, err) | ||
| } | ||
| fmt.Printf("Certificate file %s does not exist\n", certPath) | ||
| } else { | ||
| fmt.Printf("Successfully deleted certificate file: %s\n", certPath) | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| fmt.Println("Cleanup completed successfully") | ||
| return nil | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package cert | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCertCleanupCommand(t *testing.T) { | ||
| opts := &certCleanupTestOpts{} | ||
| cmd := certCleanupTestCommand(opts) | ||
| expected := &certCleanupTestOpts{ | ||
| name: "name", | ||
| } | ||
| if err := cmd.ParseFlags([]string{ | ||
| "name"}); err != nil { | ||
| t.Fatalf("Parse Flag failed: %v", err) | ||
| } | ||
| if err := cmd.Args(cmd, cmd.Flags().Args()); err != nil { | ||
| t.Fatalf("Parse Args failed: %v", err) | ||
| } | ||
| if !reflect.DeepEqual(*expected, *opts) { | ||
| t.Fatalf("Expect cert generate-test opts: %v, got: %v", expected, opts) | ||
| } | ||
| } | ||
|
|
||
| func TestCertCleanupTestCommand_MissingArgs(t *testing.T) { | ||
| cmd := certCleanupTestCommand(nil) | ||
| if err := cmd.ParseFlags(nil); err != nil { | ||
| t.Fatalf("Parse Flag failed: %v", err) | ||
| } | ||
| if err := cmd.Args(cmd, cmd.Flags().Args()); err == nil { | ||
| t.Fatal("Parse Args expected error, but ok") | ||
| } | ||
| } |
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
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
This conversation was marked as resolved.
Show resolved
Hide resolved
|
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.