This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
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 #38 from riotkit-org/issue-32-refactor-gpg-support
Refactor GPG support
- Loading branch information
Showing
30 changed files
with
448 additions
and
598 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Running make & restore locally to test encryption & sending & receiving | ||
----------------------------------------------------------------------- | ||
|
||
#### Server part | ||
|
||
1. In backup-repository repository you need to run `make k3d skaffold-deploy`, then you will have a working Backup Repository instance in local Kubernetes. | ||
|
||
2. Setup a tunnel for client connections | ||
|
||
```bash | ||
kubectl port-forward svc/server-backup-repository-server -n backups 8080:8080 | ||
``` | ||
|
||
#### Client part | ||
|
||
```bash | ||
# export basic settings. Execute once in a console | ||
export BM_COLLECTION_ID=iwa-ait | ||
export BM_PASSPHRASE=riotkit | ||
export BM_AUTH_TOKEN=$(curl -s -X POST -d '{"username":"admin","password":"admin"}' -H 'Content-Type: application/json' 'http://127.0.0.1:8080/api/stable/auth/login' | jq '.data.token' -r) | ||
export BM_URL=http://127.0.0.1:8080 | ||
``` | ||
|
||
#### Perform testing | ||
|
||
```bash | ||
./.build/backup-maker make --cmd "tar -zcvf - ./" --key ./resources/test/gpg-key.asc | ||
./.build/backup-maker restore --cmd "cat - > /tmp/restore.tar.gz" --passphrase riotkit --private-key ./resources/test/gpg-key.asc | ||
``` |
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 |
---|---|---|
|
@@ -40,7 +40,6 @@ export BM_PASSPHRASE="riotkit"; \ | |
backup-maker make --url https://example.org \ | ||
-c "tar -zcvf - ./" \ | ||
--key build/test/backup.key \ | ||
--recipient [email protected] \ | ||
--log-level info | ||
``` | ||
|
||
|
@@ -54,7 +53,6 @@ backup-maker restore --url $$(cat .build/test/domain.txt) \ | |
-c "cat - > /tmp/test" \ | ||
--private-key .build/test/backup.key \ | ||
--passphrase riotkit \ | ||
--recipient [email protected] \ | ||
--log-level debug | ||
``` | ||
|
||
|
@@ -65,25 +63,23 @@ please take a look at `Backup Controller` documentation. | |
|
||
**Note: GPG steps are optional** | ||
|
||
1. `gpg` keyring is created in a temporary directory, keys are imported | ||
1. `gpg` keys are loaded | ||
2. Command specified in `--cmd` or in `-c` is executed | ||
3. Result of the command, it's stdout is transferred to the `gpg` process | ||
4. From `gpg` process the encoded data is buffered directly to the server | ||
5. Feedback is returned | ||
6. Temporary `gpg` keyring is deleted | ||
|
||
## Restore - How it works? | ||
|
||
It is very similar as in backup operation. | ||
|
||
1. `gpg` keyring is created in a temporary directory, keys are imported | ||
1. `gpg` keys are loaded | ||
2. Command specified in `--cmd` or in `-c` is executed | ||
3. `gpg` process is started | ||
4. Backup download is starting | ||
5. Backup is transmitted on the fly from server to `gpg` -> our shell command | ||
6. Our shell `--cmd` / `-c` command is taking stdin and performing a restore action | ||
7. Feedback is returned | ||
8. Temporary `gpg` keyring is deleted | ||
|
||
## Automated procedures | ||
|
||
|
@@ -96,6 +92,7 @@ together with a tool that generates Backup & Restore procedures. Those procedure | |
|
||
- Skip `--private-key` and `--passphrase` to disable GPG | ||
- Use `debug` log level to see GPG output and more verbose output at all | ||
- Increase encryption/decryption performance by disabling armoring | ||
|
||
|
||
## Proposed usage | ||
|
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 |
---|---|---|
|
@@ -90,14 +90,11 @@ func TestDownload_SuccessWithValidGPG(t *testing.T) { | |
|
||
ctx := createExampleContext() | ||
ctx.ActionType = "download" | ||
ctx.Gpg = context.GPGOperationContext{ | ||
ctx.Crypto = context.EncryptionOperationContext{ | ||
PrivateKeyPath: "../resources/test/gpg-key.asc", | ||
Passphrase: "riotkit", | ||
Recipient: "[email protected]", | ||
EncType: "gpg-armored", | ||
} | ||
initErr := context.InitializeGPGContext(&ctx) | ||
assert.Nil(t, initErr) | ||
defer ctx.Gpg.CleanUp() | ||
|
||
_ = DownloadBackupIntoProcessStdin(ctx, "cat - > ../.build/TestDownload_SuccessWithValidGPG", client) | ||
|
||
|
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,90 @@ | ||
package encryption | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/riotkit-org/br-backup-maker/crypto" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewEncryptionCommand() *cobra.Command { | ||
app := &App{} | ||
|
||
command := &cobra.Command{ | ||
Use: "encrypt", | ||
SilenceUsage: true, | ||
Short: "Encrypts a stdin and outputs as stdout", | ||
RunE: func(command *cobra.Command, args []string) error { | ||
return app.Encrypt() | ||
}, | ||
} | ||
|
||
command.Flags().StringVarP(&app.keyPath, "key-path", "k", "", "Path to the key file") | ||
command.Flags().StringVarP(&app.encType, "type", "t", "gpg-armored", "Encryption type (options: gpg-armored, gpg-binary)") | ||
command.Flags().StringVarP(&app.passphrase, "passphrase", "p", "", "(Optional) passphrase to decrypt the key") | ||
|
||
return command | ||
} | ||
|
||
func NewDecryptionCommand() *cobra.Command { | ||
app := &App{} | ||
|
||
command := &cobra.Command{ | ||
Use: "decrypt", | ||
SilenceUsage: true, | ||
Short: "Decrypts a stdin and outputs as stdout", | ||
RunE: func(command *cobra.Command, args []string) error { | ||
return app.Decrypt() | ||
}, | ||
} | ||
|
||
command.Flags().StringVarP(&app.keyPath, "key-path", "k", "", "Path to the key file") | ||
command.Flags().StringVarP(&app.encType, "type", "t", "gpg-armored", "Encryption type (options: gpg-armored, gpg-binary)") | ||
command.Flags().StringVarP(&app.passphrase, "passphrase", "p", "", "(Optional) passphrase to decrypt the key") | ||
|
||
return command | ||
} | ||
|
||
func NewCryptoCommand() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "crypto", | ||
SilenceUsage: true, | ||
Short: "Decrypts a stdin and outputs as stdout", | ||
RunE: func(command *cobra.Command, args []string) error { | ||
return command.Help() | ||
}, | ||
} | ||
command.AddCommand(NewEncryptionCommand()) | ||
command.AddCommand(NewDecryptionCommand()) | ||
return command | ||
} | ||
|
||
type App struct { | ||
keyPath string | ||
encType string | ||
passphrase string | ||
} | ||
|
||
func (encrypt *App) createAlgo() (crypto.Service, error) { | ||
if encrypt.encType == "gpg-armored" { | ||
return crypto.GPGEncryption{Armored: true}, nil | ||
} else if encrypt.encType == "gpg-binary" { | ||
return crypto.GPGEncryption{Armored: false}, nil | ||
} | ||
return nil, errors.New("unsupported encryption type") | ||
} | ||
|
||
func (encrypt *App) Encrypt() error { | ||
algo, err := encrypt.createAlgo() | ||
if err != nil { | ||
return err | ||
} | ||
return algo.Encrypt(encrypt.keyPath, encrypt.passphrase) | ||
} | ||
|
||
func (encrypt *App) Decrypt() error { | ||
algo, err := encrypt.createAlgo() | ||
if err != nil { | ||
return err | ||
} | ||
return algo.Decrypt(encrypt.keyPath, encrypt.passphrase) | ||
} |
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.