Skip to content

Commit e681823

Browse files
feat: refactor encrypt and decrypt + CLI examples (#418)
- Refactor encrypt and decrypt - Docs - Encrypt, Decrypt - Auth --------- Co-authored-by: Jake Van Vorhis <[email protected]>
1 parent acf5702 commit e681823

File tree

15 files changed

+291
-200
lines changed

15 files changed

+291
-200
lines changed

.github/spellcheck.ignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ NPM
2222
Namespace
2323
namespace's
2424
Nano
25+
NanoTDF
2526
OIDC
2627
OpenTDF
2728
OpenID
@@ -108,8 +109,11 @@ uri
108109
with-client-creds
109110
with-client-creds-file
110111
yaml
112+
ZTDF
113+
ztdf
111114
tdo
112115
appliesToState
113116
stanag
114117
nato
115-
ocl
118+
ocl
119+
cryptographically

cmd/tdf-decrypt.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
76
"os"
@@ -14,16 +13,12 @@ import (
1413
var TDF = "tdf"
1514

1615
func dev_tdfDecryptCmd(cmd *cobra.Command, args []string) {
17-
c := cli.New(cmd, args)
16+
c := cli.New(cmd, args, cli.WithPrintJson())
1817
h := NewHandler(c)
1918
defer h.Close()
2019

2120
output := c.Flags.GetOptionalString("out")
22-
tdfType := c.Flags.GetOptionalString("tdf-type")
2321
disableAssertionVerification := c.Flags.GetOptionalBool("no-verify-assertions")
24-
if tdfType == "" {
25-
tdfType = TDF3
26-
}
2722

2823
// check for piped input
2924
piped := readPipedStdin()
@@ -40,16 +35,7 @@ func dev_tdfDecryptCmd(cmd *cobra.Command, args []string) {
4035
cli.ExitWithError("Must provide ONE of the following to decrypt: [file argument, stdin input]", errors.New("no input provided"))
4136
}
4237

43-
var decrypted *bytes.Buffer
44-
var err error
45-
switch tdfType {
46-
case TDF3:
47-
decrypted, err = h.DecryptTDF(bytesToDecrypt, disableAssertionVerification)
48-
case NANO:
49-
decrypted, err = h.DecryptNanoTDF(bytesToDecrypt)
50-
default:
51-
cli.ExitWithError("Failed to decrypt", fmt.Errorf("unrecognized tdf-type: %s", tdfType))
52-
}
38+
decrypted, err := h.DecryptBytes(bytesToDecrypt, disableAssertionVerification)
5339
if err != nil {
5440
cli.ExitWithError("Failed to decrypt file", err)
5541
}
@@ -81,6 +67,7 @@ func init() {
8167
decryptCmd.GetDocFlag("out").Default,
8268
decryptCmd.GetDocFlag("out").Description,
8369
)
70+
// deprecated flag
8471
decryptCmd.Flags().StringP(
8572
decryptCmd.GetDocFlag("tdf-type").Name,
8673
decryptCmd.GetDocFlag("tdf-type").Shorthand,

cmd/tdf-encrypt.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"fmt"
65
"io"
76
"log/slog"
@@ -16,16 +15,17 @@ import (
1615
)
1716

1817
const (
19-
TDF3 = "tdf3"
20-
NANO = "nano"
21-
Size_1MB = 1024 * 1024
18+
TDFTYPE_ZTDF = "ztdf"
19+
TDF3 = "tdf3"
20+
NANO = "nano"
21+
Size_1MB = 1024 * 1024
2222
)
2323

2424
var attrValues []string
2525
var assertions string
2626

2727
func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {
28-
c := cli.New(cmd, args)
28+
c := cli.New(cmd, args, cli.WithPrintJson())
2929
h := NewHandler(c)
3030
defer h.Close()
3131

@@ -40,9 +40,6 @@ func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {
4040
fileMimeType := c.Flags.GetOptionalString("mime-type")
4141
attrValues = c.Flags.GetStringSlice("attr", attrValues, cli.FlagsStringSliceOptions{Min: 0})
4242
tdfType := c.Flags.GetOptionalString("tdf-type")
43-
if tdfType == "" {
44-
tdfType = TDF3
45-
}
4643
kasURLPath := c.Flags.GetOptionalString("kas-url-path")
4744

4845
piped := readPipedStdin()
@@ -91,17 +88,7 @@ func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {
9188
)
9289

9390
// Do the encryption
94-
var encrypted *bytes.Buffer
95-
var err error
96-
switch tdfType {
97-
case TDF3:
98-
encrypted, err = h.EncryptBytes(bytesSlice, attrValues, fileMimeType, kasURLPath, assertions)
99-
case NANO:
100-
ecdsaBinding := c.Flags.GetOptionalBool("ecdsa-binding")
101-
encrypted, err = h.EncryptNanoBytes(bytesSlice, attrValues, kasURLPath, ecdsaBinding)
102-
default:
103-
cli.ExitWithError("Failed to encrypt", fmt.Errorf("unrecognized tdf-type: %s", tdfType))
104-
}
91+
encrypted, err := h.EncryptBytes(tdfType, bytesSlice, attrValues, fileMimeType, kasURLPath, c.Flags.GetOptionalBool("ecdsa-binding"), assertions)
10592
if err != nil {
10693
cli.ExitWithError("Failed to encrypt", err)
10794
}

docs/man/auth/_index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ command:
55
name: auth
66
---
77

8-
This command will allow you to manage your local authentication session with the OpenTDF platform.
8+
> [!NOTE]
9+
> Requires experimental profiles feature. (Linux not yet supported. Windows is brittle.)
10+
11+
The auth commands facilitate the process of authenticating the user with the system using profiles to store the
12+
credentials.

docs/man/auth/clear-client-credentials.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ command:
99
default: false
1010
---
1111

12-
This command has been deprecated. Use the `profile` subcommand to manage profiles and credentials.
12+
> [!WARNING]
13+
> Deprecated. Use the `profile` subcommand to manage profiles and credentials.

docs/man/auth/client-credentials.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,28 @@ command:
99
- client-secret
1010
---
1111

12+
> [!NOTE]
13+
> Requires experimental profiles feature.
14+
>
15+
> | OS | Keychain | State |
16+
> | --- | --- | --- |
17+
> | MacOS | Keychain | Stable |
18+
> | Windows | Credential Manager | Alpha |
19+
> | Linux | Secret Service | Not yet supported |
20+
1221
Allows the user to login in via Client Credentials flow. The client credentials will be stored safely
1322
in the OS keyring for future use.
23+
24+
## Examples
25+
26+
Authenticate with client credentials (secret provided interactively)
27+
28+
```shell
29+
opentdf auth client-credentials --client-id <client-id>
30+
```
31+
32+
Authenticate with client credentials (secret provided as argument)
33+
34+
```shell
35+
opentdf auth client-credentials --client-id <client-id> --client-secret <client-secret>
36+
```

docs/man/auth/login.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ command:
1010
required: false
1111
---
1212

13+
> [!NOTE]
14+
> Requires experimental profiles feature.
15+
>
16+
> | OS | Keychain | State |
17+
> | --- | --- | --- |
18+
> | MacOS | Keychain | Stable |
19+
> | Windows | Credential Manager | Alpha |
20+
> | Linux | Secret Service | Not yet supported |
21+
1322
Authenticate for use of the OpenTDF Platform through a browser (required).
1423

1524
Provide a specific public 'client-id' known to support the Auth Code PKCE flow and recognized

docs/man/auth/logout.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,15 @@ command:
55
name: logout
66
---
77

8+
9+
> [!NOTE]
10+
> Requires experimental profiles feature.
11+
>
12+
> | OS | Keychain | State |
13+
> | --- | --- | --- |
14+
> | MacOS | Keychain | Stable |
15+
> | Windows | Credential Manager | Alpha |
16+
> | Linux | Secret Service | Not yet supported |
17+
818
Removes any auth credentials (Client Credentials or an Access Token from a login)
919
from the current profile.

docs/man/auth/print-access-token.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ command:
99
default: false
1010
---
1111

12-
Retrieves a new OIDC Access Token using the client credentials from the OS-specific keychain and prints to stdout if found.
12+
> [!NOTE]
13+
> Requires experimental profiles feature.
14+
>
15+
> | OS | Keychain | State |
16+
> | --- | --- | --- |
17+
> | MacOS | Keychain | Stable |
18+
> | Windows | Credential Manager | Alpha |
19+
> | Linux | Secret Service | Not yet supported |
20+
21+
Retrieves a new OIDC Access Token using the client credentials and prints to stdout if found.

docs/man/decrypt/_index.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ command:
99
default: ''
1010
- name: tdf-type
1111
shorthand: t
12-
description: The type of tdf to decrypt as
13-
enum:
14-
- tdf3
15-
- nano
16-
default: tdf3
12+
description: Deprecated. TDF type is now auto-detected.
1713
- name: no-verify-assertions
1814
description: disable verification of assertions
1915
default: false
@@ -23,14 +19,24 @@ Decrypt a Trusted Data Format (TDF) file and output the contents to stdout or a
2319

2420
The first argument is the TDF file with path from the current working directory being decrypted.
2521

26-
## Examples:
22+
## Examples
2723

28-
```bash
29-
# specify the TDF to decrypt then output decrypted contents
30-
otdfctl decrypt hello.txt.tdf # write to stdout
31-
otdfctl decrypt hello.txt.tdf > hello.txt # consume stdout to write to hello.txt file
32-
otdfctl decrypt hello.txt.tdf -o hello.txt # write to hello.txt file instead of stdout
24+
Various ways to decrypt a TDF file
3325

34-
# pipe the TDF to decrypt
35-
cat hello.txt.tdf | otdfctl decrypt > hello.txt
26+
```shell
27+
# decrypt file and write to standard output
28+
otdfctl decrypt hello.txt.tdf
29+
30+
# decrypt file and write to hello.txt file
31+
otdfctl decrypt hello.txt.tdf -o hello.txt
32+
33+
# decrypt piped TDF content and write to hello.txt file
34+
cat hello.txt.tdf | otdfctl decrypt -o hello.txt
35+
```
36+
37+
Advanced piping is supported
38+
39+
```shell
40+
$ echo "hello world" | otdfctl encrypt | otdfctl decrypt | cat
41+
hello world
3642
```

0 commit comments

Comments
 (0)