@znemz/aws-play 0.1.52 → 0.1.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/aws/index.js +17 -15
- package/aws/kms/args/{main.go → args.go} +7 -2
- package/aws/kms/args/index.js +5 -0
- package/aws/kms/decrypt/cli.js +14 -1
- package/aws/kms/decrypt/index.js +27 -20
- package/aws/kms/decrypt/main.go +22 -23
- package/aws/kms/encrypt/cli.js +15 -1
- package/aws/kms/encrypt/index.js +21 -19
- package/aws/kms/encrypt/main.go +22 -25
- package/package.json +11 -5
- package/aws/debug.go +0 -7
package/README.md
CHANGED
|
@@ -20,6 +20,6 @@ AQICAHj/6a1KHdB7qaXDbeWQ9K48M0vQfukO9weGdqwlCJ2ehQE2GJx31AA8adTIcCOKmJf9AAAAYzBh
|
|
|
20
20
|
`go get github.com/nmccready/aws-play/aws/kms/decrypt`
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
$ echo abcd | encrypt -e base64 | decrypt -e base64
|
|
23
|
+
$ echo abcd | encrypt -e base64 -k alias/demo | decrypt -e base64
|
|
24
24
|
abcd
|
|
25
25
|
```
|
package/aws/index.js
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const { fromSSO } = require('@aws-sdk/credential-providers');
|
|
2
|
+
const { KMSClient } = require("@aws-sdk/client-kms");
|
|
3
|
+
const { addProxyToClient } = require("aws-sdk-v3-proxy");
|
|
3
4
|
|
|
4
5
|
const { HTTP_PROXY, HTTPS_PROXY } = process.env;
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
AWS.config.update({
|
|
8
|
-
...rest,
|
|
9
|
-
httpOptions: {
|
|
10
|
-
agent: proxyFact(proxy),
|
|
11
|
-
},
|
|
12
|
-
});
|
|
13
|
-
return AWS;
|
|
14
|
-
};
|
|
7
|
+
const debug = require('../debug').spawn('aws:kms:factory');
|
|
15
8
|
|
|
16
9
|
const initEnv = () => {
|
|
17
10
|
const proxy = HTTPS_PROXY || HTTP_PROXY;
|
|
18
11
|
if (proxy) {
|
|
19
|
-
|
|
12
|
+
debug(() => `Using proxy: ${proxy}`);
|
|
13
|
+
return addProxyToClient(new KMSClient());
|
|
14
|
+
}
|
|
15
|
+
if (process.env.AWS_SSO_SESSION || process.env.AWS_PROFILE) {
|
|
16
|
+
debug(() => 'Using SSO credentials');
|
|
17
|
+
return new KMSClient({
|
|
18
|
+
credentials: fromSSO(),
|
|
19
|
+
});
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
debug(() => 'No proxy configured, using default KMS client');
|
|
22
|
+
return new KMSClient();
|
|
22
23
|
};
|
|
23
24
|
|
|
25
|
+
const getClient = initEnv;
|
|
26
|
+
|
|
24
27
|
module.exports = {
|
|
25
|
-
|
|
26
|
-
init,
|
|
28
|
+
getClient,
|
|
27
29
|
initEnv,
|
|
28
30
|
};
|
|
@@ -3,10 +3,11 @@ package args
|
|
|
3
3
|
import (
|
|
4
4
|
"errors"
|
|
5
5
|
"flag"
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
"github.com/nmccready/aws-play/internal/logger"
|
|
7
8
|
)
|
|
8
9
|
|
|
9
|
-
var debug = Spawn("args")
|
|
10
|
+
var debug = logger.Spawn("args")
|
|
10
11
|
|
|
11
12
|
type Args struct {
|
|
12
13
|
Encoding string
|
|
@@ -41,6 +42,10 @@ func GetArgs() *Args {
|
|
|
41
42
|
|
|
42
43
|
flag.Parse()
|
|
43
44
|
|
|
45
|
+
if args.Encoding == "" {
|
|
46
|
+
args.Encoding = "base64" // default encoding
|
|
47
|
+
}
|
|
48
|
+
|
|
44
49
|
debug.Log("args: %+v", args)
|
|
45
50
|
|
|
46
51
|
return &args
|
package/aws/kms/args/index.js
CHANGED
|
@@ -23,6 +23,11 @@ const getArgs = () => {
|
|
|
23
23
|
type: 'bool',
|
|
24
24
|
description:
|
|
25
25
|
'for decrypt which defaults to false IE uses first key that works, this is to foce a specific key usage',
|
|
26
|
+
})
|
|
27
|
+
.option('data', {
|
|
28
|
+
alias: 'd',
|
|
29
|
+
type: 'string',
|
|
30
|
+
description: 'data to encrypt or decrypt, defaults to stdin',
|
|
26
31
|
});
|
|
27
32
|
|
|
28
33
|
debug(() => argv);
|
package/aws/kms/decrypt/cli.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const { decrypt } = require('./index');
|
|
2
|
+
const { decrypt, decryptAsync } = require('./index');
|
|
3
|
+
const getArgs = require('../args');
|
|
4
|
+
const args = getArgs();
|
|
5
|
+
|
|
6
|
+
if (args.data) {
|
|
7
|
+
decryptAsync(args.data, args.keyId, (err, result) => {
|
|
8
|
+
if (err) {
|
|
9
|
+
console.error('Decryption error:', err);
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
console.log(result);
|
|
13
|
+
process.exit(0);
|
|
14
|
+
})
|
|
15
|
+
}
|
|
3
16
|
|
|
4
17
|
process.stdin.pipe(decrypt).pipe(process.stdout).once('error', console.error);
|
package/aws/kms/decrypt/index.js
CHANGED
|
@@ -1,41 +1,48 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const through = require('through2');
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const { KMS } = initEnv();
|
|
4
|
+
const { DecryptCommand } = require("@aws-sdk/client-kms");
|
|
5
|
+
const kms = require('../..');
|
|
6
|
+
const args = require('../args')();
|
|
8
7
|
|
|
9
|
-
const
|
|
10
|
-
const kms = new KMS();
|
|
8
|
+
const debug = require('../../../debug').spawn('aws:kms:decrypt');
|
|
11
9
|
|
|
12
10
|
const decoders = {
|
|
13
11
|
default: (data) => data, // pass through
|
|
14
12
|
decode: (encoding) => (data) => Buffer.from(data, encoding),
|
|
15
13
|
};
|
|
16
14
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
const decryptAsync = async (toDecrypt, keyId = process.env.KMS_ID, cb) => {
|
|
16
|
+
try {
|
|
17
|
+
const client = kms.getClient();
|
|
18
|
+
debug(() => `Decrypting data with key ${keyId}`);
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
const decoder = args.encoding ? decoders.decode(args.encoding) : decoders.default;
|
|
21
|
+
toDecrypt = decoder(String(toDecrypt));
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (
|
|
26
|
-
|
|
23
|
+
const opts = { CiphertextBlob: toDecrypt };
|
|
24
|
+
|
|
25
|
+
if (args.forceKeyId) {
|
|
26
|
+
if (keyId) {
|
|
27
|
+
opts.KeyId = keyId;
|
|
28
|
+
}
|
|
27
29
|
}
|
|
30
|
+
|
|
31
|
+
const data = await client.send(new DecryptCommand(opts));
|
|
32
|
+
debug(() => `Decrypted data with key ${keyId}`);
|
|
33
|
+
cb(null, Buffer.from(data.Plaintext).toString('utf8'));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
debug(() => `Decryption failed: ${err.message}`);
|
|
36
|
+
cb(err);
|
|
28
37
|
}
|
|
38
|
+
}
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
cb(err);
|
|
33
|
-
}
|
|
34
|
-
cb(null, data.Plaintext);
|
|
35
|
-
});
|
|
40
|
+
const decrypt = through.obj((toDecrypt, _, cb) => {
|
|
41
|
+
decryptAsync(toDecrypt, args['key-id'], cb);
|
|
36
42
|
});
|
|
37
43
|
|
|
38
44
|
module.exports = {
|
|
39
45
|
decoders,
|
|
40
46
|
decrypt,
|
|
47
|
+
decryptAsync,
|
|
41
48
|
};
|
package/aws/kms/decrypt/main.go
CHANGED
|
@@ -2,18 +2,19 @@ package main
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"bufio"
|
|
5
|
+
"context"
|
|
5
6
|
"encoding/base64"
|
|
6
7
|
"encoding/hex"
|
|
7
8
|
"fmt"
|
|
8
9
|
"os"
|
|
9
10
|
|
|
10
|
-
"github.com/aws/aws-sdk-go/
|
|
11
|
-
|
|
12
|
-
"github.com/
|
|
13
|
-
|
|
11
|
+
"github.com/aws/aws-sdk-go-v2/config"
|
|
12
|
+
"github.com/aws/aws-sdk-go-v2/service/kms"
|
|
13
|
+
"github.com/nmccready/aws-play/aws/kms/args"
|
|
14
|
+
"github.com/nmccready/aws-sdk-go-v2-ifaces/service/kms/kms_iface"
|
|
14
15
|
)
|
|
15
16
|
|
|
16
|
-
func
|
|
17
|
+
func DecryptWithClient(client kms_iface.IClient, text string, args *args.Args) (string, error) {
|
|
17
18
|
var err error
|
|
18
19
|
var b []byte
|
|
19
20
|
|
|
@@ -31,47 +32,45 @@ func Decrypt(text string, args *Args) (string, error) {
|
|
|
31
32
|
}
|
|
32
33
|
text = string(b)
|
|
33
34
|
}
|
|
34
|
-
session, err := NewSession()
|
|
35
|
-
|
|
36
|
-
if err != nil {
|
|
37
|
-
return "", err
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
svc := kms.New(session)
|
|
41
35
|
|
|
42
36
|
var keyId *string
|
|
43
|
-
|
|
44
37
|
if args.ForceKeyId {
|
|
45
38
|
maybeKey := os.Getenv("KMS_ID")
|
|
46
|
-
|
|
47
39
|
if args.KeyId != "" {
|
|
48
40
|
maybeKey = args.KeyId
|
|
49
41
|
}
|
|
50
|
-
|
|
51
42
|
if maybeKey != "" {
|
|
52
|
-
keyId =
|
|
43
|
+
keyId = &maybeKey
|
|
53
44
|
}
|
|
54
45
|
}
|
|
55
46
|
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
input := &kms.DecryptInput{
|
|
48
|
+
KeyId: keyId,
|
|
49
|
+
CiphertextBlob: []byte(text),
|
|
50
|
+
}
|
|
51
|
+
out, err := client.Decrypt(context.Background(), input)
|
|
58
52
|
if err != nil {
|
|
59
53
|
return "", err
|
|
60
54
|
}
|
|
61
|
-
|
|
62
55
|
return string(out.Plaintext), nil
|
|
63
56
|
}
|
|
64
57
|
|
|
58
|
+
func Decrypt(text string, args *args.Args) (string, error) {
|
|
59
|
+
cfg, err := config.LoadDefaultConfig(context.Background())
|
|
60
|
+
if err != nil {
|
|
61
|
+
return "", err
|
|
62
|
+
}
|
|
63
|
+
client := kms.NewFromConfig(cfg)
|
|
64
|
+
return DecryptWithClient(client, text, args)
|
|
65
|
+
}
|
|
66
|
+
|
|
65
67
|
func main() {
|
|
66
68
|
reader := bufio.NewReader(os.Stdin)
|
|
67
69
|
text, _ := reader.ReadString('\n')
|
|
68
|
-
|
|
69
|
-
args := GetArgs()
|
|
70
|
+
args := args.GetArgs()
|
|
70
71
|
out, err := Decrypt(text, args)
|
|
71
|
-
|
|
72
72
|
if err != nil {
|
|
73
73
|
panic(err)
|
|
74
74
|
}
|
|
75
|
-
|
|
76
75
|
fmt.Print(out)
|
|
77
76
|
}
|
package/aws/kms/encrypt/cli.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const { encrypt } = require('./index');
|
|
2
|
+
const { encrypt, encryptAsync } = require('./index');
|
|
3
|
+
const getArgs = require('../args');
|
|
4
|
+
|
|
5
|
+
const args = getArgs();
|
|
6
|
+
|
|
7
|
+
if (args.data) {
|
|
8
|
+
encryptAsync(args.data, args.keyId, (err, result) => {
|
|
9
|
+
if (err) {
|
|
10
|
+
console.error('Encryption error:', err);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
console.log(result);
|
|
14
|
+
process.exit(0);
|
|
15
|
+
})
|
|
16
|
+
}
|
|
3
17
|
|
|
4
18
|
process.stdin.pipe(encrypt).pipe(process.stdout);
|
package/aws/kms/encrypt/index.js
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
1
|
const through = require('through2');
|
|
2
|
-
|
|
3
|
-
const
|
|
2
|
+
const { EncryptCommand } = require("@aws-sdk/client-kms");
|
|
3
|
+
const kms = require('../..');
|
|
4
4
|
const getArgs = require('../args');
|
|
5
|
-
const { KMS } = initEnv();
|
|
6
5
|
|
|
7
6
|
const args = getArgs();
|
|
8
|
-
const
|
|
7
|
+
const debug = require('../../../debug').spawn('aws:kms:encrypt');
|
|
9
8
|
|
|
10
9
|
const encoders = {
|
|
11
10
|
default: (data) => data, // pass through
|
|
12
11
|
encode: (encoding) => (data) => Buffer.from(data, 'utf8').toString(encoding),
|
|
13
12
|
};
|
|
14
13
|
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
const encryptAsync = async (toEncrypt, keyId = process.env.KMS_ID, cb) => {
|
|
15
|
+
try {
|
|
16
|
+
const client = kms.getClient();
|
|
17
|
+
debug(() => `Encrypting data with key ${keyId}`);
|
|
18
|
+
const data = await client.send(new EncryptCommand({ KeyId: keyId, Plaintext: toEncrypt }));
|
|
19
|
+
debug(() => `Encrypted data with key ${keyId}`);
|
|
20
|
+
const encoder = args.encoding ? encoders.encode(args.encoding) : encoders.default;
|
|
21
|
+
const encrypted = encoder(data.CiphertextBlob);
|
|
22
|
+
cb(null, encrypted);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
debug(() => `Encryption failed: ${err.message}`);
|
|
25
|
+
cb(err);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
cb(null, encrypted);
|
|
29
|
-
}
|
|
30
|
-
);
|
|
29
|
+
const encrypt = through.obj((toEncrypt, _, cb) => {
|
|
30
|
+
encryptAsync(toEncrypt, args['key-id'], cb)
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
+
|
|
33
34
|
module.exports = {
|
|
34
35
|
encoders,
|
|
35
36
|
encrypt,
|
|
37
|
+
encryptAsync,
|
|
36
38
|
};
|
package/aws/kms/encrypt/main.go
CHANGED
|
@@ -7,36 +7,27 @@ import (
|
|
|
7
7
|
"fmt"
|
|
8
8
|
"os"
|
|
9
9
|
|
|
10
|
-
"
|
|
11
|
-
. "github.com/aws/aws-sdk-go/aws/session"
|
|
12
|
-
"github.com/aws/aws-sdk-go/service/kms"
|
|
13
|
-
. "github.com/nmccready/aws-play/aws/kms/args"
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
func Encrypt(text string, args *Args) (string, error) {
|
|
17
|
-
session, err := NewSession()
|
|
18
|
-
|
|
19
|
-
if err != nil {
|
|
20
|
-
return "", err
|
|
21
|
-
}
|
|
10
|
+
"context"
|
|
22
11
|
|
|
23
|
-
|
|
12
|
+
"github.com/aws/aws-sdk-go-v2/config"
|
|
13
|
+
"github.com/aws/aws-sdk-go-v2/service/kms"
|
|
14
|
+
"github.com/nmccready/aws-play/aws/kms/args"
|
|
15
|
+
"github.com/nmccready/aws-sdk-go-v2-ifaces/service/kms/kms_iface"
|
|
16
|
+
)
|
|
24
17
|
|
|
18
|
+
func EncryptWithClient(client kms_iface.IClient, text string, args *args.Args) (string, error) {
|
|
25
19
|
keyId := os.Getenv("KMS_ID")
|
|
26
|
-
|
|
27
|
-
if args.KeyId != "" {
|
|
20
|
+
if args != nil && args.KeyId != "" {
|
|
28
21
|
keyId = args.KeyId
|
|
29
22
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
KeyId: aws.String(keyId),
|
|
23
|
+
input := &kms.EncryptInput{
|
|
24
|
+
KeyId: &keyId,
|
|
33
25
|
Plaintext: []byte(text),
|
|
34
|
-
}
|
|
35
|
-
|
|
26
|
+
}
|
|
27
|
+
out, err := client.Encrypt(context.Background(), input)
|
|
36
28
|
if err != nil {
|
|
37
29
|
return "", err
|
|
38
30
|
}
|
|
39
|
-
|
|
40
31
|
if args == nil || args.Encoding == "" {
|
|
41
32
|
return string(out.CiphertextBlob), nil
|
|
42
33
|
}
|
|
@@ -46,16 +37,22 @@ func Encrypt(text string, args *Args) (string, error) {
|
|
|
46
37
|
return hex.EncodeToString(out.CiphertextBlob), nil
|
|
47
38
|
}
|
|
48
39
|
|
|
40
|
+
func Encrypt(text string, args *args.Args) (string, error) {
|
|
41
|
+
cfg, err := config.LoadDefaultConfig(context.Background())
|
|
42
|
+
if err != nil {
|
|
43
|
+
return "", err
|
|
44
|
+
}
|
|
45
|
+
client := kms.NewFromConfig(cfg)
|
|
46
|
+
return EncryptWithClient(client, text, args)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
49
|
func main() {
|
|
50
50
|
reader := bufio.NewReader(os.Stdin)
|
|
51
51
|
text, _ := reader.ReadString('\n')
|
|
52
|
-
|
|
53
|
-
args := GetArgs()
|
|
52
|
+
args := args.GetArgs()
|
|
54
53
|
out, err := Encrypt(text, args)
|
|
55
|
-
|
|
56
54
|
if err != nil {
|
|
57
55
|
panic(err)
|
|
58
56
|
}
|
|
59
|
-
|
|
60
57
|
fmt.Print(out)
|
|
61
58
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@znemz/aws-play",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "",
|
|
@@ -13,20 +13,26 @@
|
|
|
13
13
|
"debug.js"
|
|
14
14
|
],
|
|
15
15
|
"scripts": {
|
|
16
|
-
"lint": "
|
|
16
|
+
"lint": "npm run lint:go && npm run lint:js",
|
|
17
|
+
"lint:go": "golangci-lint run --fix",
|
|
18
|
+
"lint:js": "eslint . --ext .js,.ts,.mjs,.cjs",
|
|
17
19
|
"prepare": "go mod tidy -e && go mod vendor && sort-package-json",
|
|
18
20
|
"test": "npm run test:go",
|
|
19
21
|
"test:go": "go test ./aws/..."
|
|
20
22
|
},
|
|
21
23
|
"dependencies": {
|
|
22
|
-
"aws-sdk": "^
|
|
24
|
+
"@aws-sdk/client-kms": "^3.400.0",
|
|
25
|
+
"@aws-sdk/credential-providers": "^3.400.0",
|
|
26
|
+
"aws-sdk-v3-proxy": "2.1.4",
|
|
23
27
|
"debug-fabulous": "^2.0.1",
|
|
24
|
-
"proxy-agent": "6.5.0",
|
|
25
28
|
"through2": "^4.0.2",
|
|
26
29
|
"yargs": "^17.7.2"
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
|
-
"
|
|
32
|
+
"@eslint/js": "9.27.0",
|
|
33
|
+
"@znemz/aws-play": "file:./",
|
|
34
|
+
"eslint": "9.27.0",
|
|
35
|
+
"sort-package-json": "3.0",
|
|
30
36
|
"standard-version": "9.5"
|
|
31
37
|
}
|
|
32
38
|
}
|