himari-aws 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +171 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/Rakefile +8 -0
- data/lib/himari/aws/dynamodb_storage.rb +69 -0
- data/lib/himari/aws/secretsmanager_signing_key_provider.rb +79 -0
- data/lib/himari/aws/secretsmanager_signing_key_rotation_handler.rb +123 -0
- data/lib/himari/aws/version.rb +7 -0
- data/lib/himari/aws.rb +6 -0
- data/lib/himari-aws.rb +1 -0
- data/sig/himari/aws.rbs +6 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: eb42481e34029c9e1ae8c3594dd64995f423a53818cbc867a0fd2c65c64c3ff1
|
4
|
+
data.tar.gz: 55e743a182643484f59544428a9384661a161d4a5657895bd9bed70714c19024
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc863a5752789f4b83b030c5bb44fc6de47d52acdc44225baf74d97e04f4c2de6ff3b3e1a4fb03bde80b377c956bd70137c8dc0d2cdb2c2e33238d107644c4c1
|
7
|
+
data.tar.gz: d5093ed3e72bae790f76b9709443b37bf97be5686b53b676cb35fc1d01568685c93d228a1c9825157480328439d6c638e8846afb746a522f9a24320fe61239d8
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../himari
|
3
|
+
specs:
|
4
|
+
himari (0.1.0)
|
5
|
+
addressable
|
6
|
+
omniauth (>= 2.0)
|
7
|
+
openid_connect
|
8
|
+
rack-oauth2
|
9
|
+
rack-protection
|
10
|
+
sinatra (>= 3.0)
|
11
|
+
|
12
|
+
PATH
|
13
|
+
remote: .
|
14
|
+
specs:
|
15
|
+
himari-aws (0.1.0)
|
16
|
+
aws-sdk-dynamodb
|
17
|
+
aws-sdk-secretsmanager
|
18
|
+
himari
|
19
|
+
|
20
|
+
GEM
|
21
|
+
remote: https://rubygems.org/
|
22
|
+
specs:
|
23
|
+
activemodel (7.0.4.3)
|
24
|
+
activesupport (= 7.0.4.3)
|
25
|
+
activesupport (7.0.4.3)
|
26
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
27
|
+
i18n (>= 1.6, < 2)
|
28
|
+
minitest (>= 5.1)
|
29
|
+
tzinfo (~> 2.0)
|
30
|
+
addressable (2.8.1)
|
31
|
+
public_suffix (>= 2.0.2, < 6.0)
|
32
|
+
aes_key_wrap (1.1.0)
|
33
|
+
attr_required (1.0.1)
|
34
|
+
aws-eventstream (1.2.0)
|
35
|
+
aws-partitions (1.730.0)
|
36
|
+
aws-sdk-core (3.170.1)
|
37
|
+
aws-eventstream (~> 1, >= 1.0.2)
|
38
|
+
aws-partitions (~> 1, >= 1.651.0)
|
39
|
+
aws-sigv4 (~> 1.5)
|
40
|
+
jmespath (~> 1, >= 1.6.1)
|
41
|
+
aws-sdk-dynamodb (1.83.0)
|
42
|
+
aws-sdk-core (~> 3, >= 3.165.0)
|
43
|
+
aws-sigv4 (~> 1.1)
|
44
|
+
aws-sdk-secretsmanager (1.73.0)
|
45
|
+
aws-sdk-core (~> 3, >= 3.165.0)
|
46
|
+
aws-sigv4 (~> 1.1)
|
47
|
+
aws-sigv4 (1.5.2)
|
48
|
+
aws-eventstream (~> 1, >= 1.0.2)
|
49
|
+
bindata (2.4.15)
|
50
|
+
concurrent-ruby (1.2.2)
|
51
|
+
date (3.3.3)
|
52
|
+
diff-lcs (1.5.0)
|
53
|
+
faraday (2.7.4)
|
54
|
+
faraday-net_http (>= 2.0, < 3.1)
|
55
|
+
ruby2_keywords (>= 0.0.4)
|
56
|
+
faraday-follow_redirects (0.3.0)
|
57
|
+
faraday (>= 1, < 3)
|
58
|
+
faraday-net_http (3.0.2)
|
59
|
+
hashie (5.0.0)
|
60
|
+
i18n (1.12.0)
|
61
|
+
concurrent-ruby (~> 1.0)
|
62
|
+
jmespath (1.6.2)
|
63
|
+
json-jwt (1.16.3)
|
64
|
+
activesupport (>= 4.2)
|
65
|
+
aes_key_wrap
|
66
|
+
bindata
|
67
|
+
faraday (~> 2.0)
|
68
|
+
faraday-follow_redirects
|
69
|
+
mail (2.8.1)
|
70
|
+
mini_mime (>= 0.1.1)
|
71
|
+
net-imap
|
72
|
+
net-pop
|
73
|
+
net-smtp
|
74
|
+
mini_mime (1.1.2)
|
75
|
+
mini_portile2 (2.8.1)
|
76
|
+
minitest (5.18.0)
|
77
|
+
mustermann (3.0.0)
|
78
|
+
ruby2_keywords (~> 0.0.1)
|
79
|
+
net-imap (0.3.4)
|
80
|
+
date
|
81
|
+
net-protocol
|
82
|
+
net-pop (0.1.2)
|
83
|
+
net-protocol
|
84
|
+
net-protocol (0.2.1)
|
85
|
+
timeout
|
86
|
+
net-smtp (0.3.3)
|
87
|
+
net-protocol
|
88
|
+
nokogiri (1.14.2)
|
89
|
+
mini_portile2 (~> 2.8.0)
|
90
|
+
racc (~> 1.4)
|
91
|
+
omniauth (2.1.1)
|
92
|
+
hashie (>= 3.4.6)
|
93
|
+
rack (>= 2.2.3)
|
94
|
+
rack-protection
|
95
|
+
openid_connect (2.2.0)
|
96
|
+
activemodel
|
97
|
+
attr_required (>= 1.0.0)
|
98
|
+
faraday (~> 2.0)
|
99
|
+
faraday-follow_redirects
|
100
|
+
json-jwt (>= 1.16)
|
101
|
+
net-smtp
|
102
|
+
rack-oauth2 (~> 2.2)
|
103
|
+
swd (~> 2.0)
|
104
|
+
tzinfo
|
105
|
+
validate_email
|
106
|
+
validate_url
|
107
|
+
webfinger (~> 2.0)
|
108
|
+
public_suffix (5.0.1)
|
109
|
+
racc (1.6.2)
|
110
|
+
rack (2.2.6.4)
|
111
|
+
rack-oauth2 (2.2.0)
|
112
|
+
activesupport
|
113
|
+
attr_required
|
114
|
+
faraday (~> 2.0)
|
115
|
+
faraday-follow_redirects
|
116
|
+
json-jwt (>= 1.11.0)
|
117
|
+
rack (>= 2.1.0)
|
118
|
+
rack-protection (3.0.5)
|
119
|
+
rack
|
120
|
+
rake (13.0.6)
|
121
|
+
rspec (3.12.0)
|
122
|
+
rspec-core (~> 3.12.0)
|
123
|
+
rspec-expectations (~> 3.12.0)
|
124
|
+
rspec-mocks (~> 3.12.0)
|
125
|
+
rspec-core (3.12.1)
|
126
|
+
rspec-support (~> 3.12.0)
|
127
|
+
rspec-expectations (3.12.2)
|
128
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
129
|
+
rspec-support (~> 3.12.0)
|
130
|
+
rspec-mocks (3.12.4)
|
131
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
132
|
+
rspec-support (~> 3.12.0)
|
133
|
+
rspec-support (3.12.0)
|
134
|
+
ruby2_keywords (0.0.5)
|
135
|
+
sinatra (3.0.5)
|
136
|
+
mustermann (~> 3.0)
|
137
|
+
rack (~> 2.2, >= 2.2.4)
|
138
|
+
rack-protection (= 3.0.5)
|
139
|
+
tilt (~> 2.0)
|
140
|
+
swd (2.0.2)
|
141
|
+
activesupport (>= 3)
|
142
|
+
attr_required (>= 0.0.5)
|
143
|
+
faraday (~> 2.0)
|
144
|
+
faraday-follow_redirects
|
145
|
+
tilt (2.1.0)
|
146
|
+
timeout (0.3.2)
|
147
|
+
tzinfo (2.0.6)
|
148
|
+
concurrent-ruby (~> 1.0)
|
149
|
+
validate_email (0.1.6)
|
150
|
+
activemodel (>= 3.0)
|
151
|
+
mail (>= 2.2.5)
|
152
|
+
validate_url (1.0.15)
|
153
|
+
activemodel (>= 3.0.0)
|
154
|
+
public_suffix
|
155
|
+
webfinger (2.1.2)
|
156
|
+
activesupport
|
157
|
+
faraday (~> 2.0)
|
158
|
+
faraday-follow_redirects
|
159
|
+
|
160
|
+
PLATFORMS
|
161
|
+
ruby
|
162
|
+
|
163
|
+
DEPENDENCIES
|
164
|
+
himari!
|
165
|
+
himari-aws!
|
166
|
+
nokogiri
|
167
|
+
rake (~> 13.0)
|
168
|
+
rspec (~> 3.0)
|
169
|
+
|
170
|
+
BUNDLED WITH
|
171
|
+
2.4.8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Sorah Fukumori
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Himari::Aws
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/himari/aws`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/himari-aws.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'himari/storages/base'
|
2
|
+
require 'aws-sdk-dynamodb'
|
3
|
+
|
4
|
+
module Himari
|
5
|
+
module Aws
|
6
|
+
class DynamodbStorage
|
7
|
+
include Himari::Storages::Base
|
8
|
+
|
9
|
+
# @param client [Aws::DynamoDB::Client]
|
10
|
+
# @param table_name [String] name of DynamoDB table with hash=pk/range=sk key.
|
11
|
+
def initialize(client: ::Aws::DynamoDB::Client.new, table_name:)
|
12
|
+
@client = client
|
13
|
+
@table_name = table_name
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :client, :table_name
|
17
|
+
|
18
|
+
private def write(kind, key, content, overwrite: false)
|
19
|
+
pk = "storage:#{kind}:#{key}"
|
20
|
+
payload = {
|
21
|
+
content_json: JSON.pretty_generate(content),
|
22
|
+
ttl: content[:ttl] || content[:expiry],
|
23
|
+
}
|
24
|
+
@client.update_item(
|
25
|
+
table_name: @table_name,
|
26
|
+
key: {
|
27
|
+
'pk' => pk,
|
28
|
+
'sk' => pk,
|
29
|
+
},
|
30
|
+
# #{payload.each_key.map { |k| "##{k} = :#{k}" }.join(', ')}
|
31
|
+
update_expression: <<~EOS,
|
32
|
+
SET
|
33
|
+
#content_json = :content_json
|
34
|
+
#{payload[:ttl] ? ", #ttl = :ttl" : "REMOVE #ttl"}
|
35
|
+
EOS
|
36
|
+
condition_expression: overwrite ? nil : 'attribute_not_exists(pk)',
|
37
|
+
expression_attribute_names: payload.each_key.map { |k| ["##{k}", k] }.to_h,
|
38
|
+
expression_attribute_values: payload.transform_keys { ":#{_1}" },
|
39
|
+
)
|
40
|
+
nil
|
41
|
+
rescue ::Aws::DynamoDB::Errors::ConditionalCheckFailedException
|
42
|
+
raise Himari::Storages::Base::Conflict
|
43
|
+
end
|
44
|
+
|
45
|
+
private def read(kind, key)
|
46
|
+
pk = "storage:#{kind}:#{key}"
|
47
|
+
item = @client.query(
|
48
|
+
table_name: @table_name,
|
49
|
+
select: 'ALL_ATTRIBUTES',
|
50
|
+
limit: 1,
|
51
|
+
key_condition_expression: 'pk = :pk AND sk = :sk',
|
52
|
+
expression_attribute_values: {":pk" => pk, ":sk" => pk},
|
53
|
+
).items.first
|
54
|
+
|
55
|
+
return nil unless item
|
56
|
+
JSON.parse(item.fetch('content_json'), symbolize_names: true)
|
57
|
+
end
|
58
|
+
|
59
|
+
private def delete(kind, key)
|
60
|
+
pk = "storage:#{kind}:#{key}"
|
61
|
+
@client.delete_item(
|
62
|
+
table_name: @table_name,
|
63
|
+
key: {'pk' => pk, 'sk' => pk}
|
64
|
+
)
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'aws-sdk-secretsmanager'
|
2
|
+
require 'himari/signing_key'
|
3
|
+
require 'himari/middlewares/signing_key'
|
4
|
+
|
5
|
+
module Himari
|
6
|
+
module Aws
|
7
|
+
class SecretsmanagerSigningKeyProvider
|
8
|
+
def initialize(app, kwargs = {})
|
9
|
+
@app = app
|
10
|
+
@inner = Provider.new(**kwargs)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
env[Himari::Middlewares::SigningKey::RACK_KEY] ||= []
|
15
|
+
env[Himari::Middlewares::SigningKey::RACK_KEY] += [@inner]
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
|
19
|
+
class Provider
|
20
|
+
def initialize(client: ::Aws::SecretsManager::Client.new, secret_id:, group: nil, kid_prefix:)
|
21
|
+
@client = client
|
22
|
+
@secret_id = secret_id
|
23
|
+
@group = group
|
24
|
+
@kid_prefix = kid_prefix
|
25
|
+
end
|
26
|
+
|
27
|
+
def collect(id: nil, active: nil, group: nil, **_remainder)
|
28
|
+
return [] if group && group != @group
|
29
|
+
case
|
30
|
+
when id
|
31
|
+
return [] unless id.start_with?("#{@kid_prefix}_")
|
32
|
+
version_id = id[(@kid_prefix.size+1)..-1] || ''
|
33
|
+
[secret_value_to_signing_key(@client.get_secret_value(secret_id: @secret_id, version_id: version_id))].compact
|
34
|
+
|
35
|
+
when active
|
36
|
+
[secret_value_to_signing_key(@client.get_secret_value(secret_id: @secret_id, version_stage: 'AWSCURRENT'))].compact
|
37
|
+
|
38
|
+
else
|
39
|
+
values = @client.describe_secret(secret_id: @secret_id)
|
40
|
+
.then { |secret| [secret, secret.version_ids_to_stages.keys] }
|
41
|
+
.then { |(secret, versions)| versions.map { |v| @client.get_secret_value(secret_id: secret.arn, version_id: v) } }
|
42
|
+
values.map { |v| secret_value_to_signing_key(v) }.compact
|
43
|
+
end
|
44
|
+
rescue ::Aws::SecretsManager::Errors::ResourceNotFoundException
|
45
|
+
[]
|
46
|
+
end
|
47
|
+
|
48
|
+
private def secret_value_to_signing_key(value, inactive: false)
|
49
|
+
json = begin
|
50
|
+
JSON.parse(value.secret_string)
|
51
|
+
rescue JSON::ParserError
|
52
|
+
warn "JSON::ParserError while parsing #{value.arn} #{value.version_id}"
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
|
56
|
+
return nil unless json['kind'] == 'himari.signing_key'
|
57
|
+
|
58
|
+
pkey = case json.fetch('kty')
|
59
|
+
when 'rsa'
|
60
|
+
OpenSSL::PKey::RSA.new(json.fetch('rsa').fetch('pem'), '')
|
61
|
+
when 'ec'
|
62
|
+
OpenSSL::PKey::EC.new(json.fetch('ec').fetch('pem'), '')
|
63
|
+
else
|
64
|
+
raise "#{value.arn} #{value.version_id} has invalid kty"
|
65
|
+
end
|
66
|
+
|
67
|
+
Himari::SigningKey.new(
|
68
|
+
id: "#{@kid_prefix}_#{value.version_id}",
|
69
|
+
pkey: pkey,
|
70
|
+
alg: json.fetch('alg', nil),
|
71
|
+
group: @group,
|
72
|
+
inactive: !value.version_stages.include?('AWSCURRENT'),
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotate-secrets_how.html
|
2
|
+
require 'openssl'
|
3
|
+
require 'json'
|
4
|
+
require 'aws-sdk-secretsmanager'
|
5
|
+
|
6
|
+
# this file should be independently loadable to ease lambda deployment
|
7
|
+
|
8
|
+
module Himari
|
9
|
+
module Aws
|
10
|
+
module SecretsmanagerSigningKeyRotationHandler
|
11
|
+
RotationRequest = Struct.new(:step, :id, :token, :secret, keyword_init: true)
|
12
|
+
|
13
|
+
def self.handler(event:, context:)
|
14
|
+
@secretsmanager ||= ::Aws::SecretsManager::Client.new()
|
15
|
+
|
16
|
+
secret = prerequisite_check!(event)
|
17
|
+
|
18
|
+
req = RotationRequest.new(
|
19
|
+
step: event.fetch('Step'),
|
20
|
+
token: event.fetch('ClientRequestToken'),
|
21
|
+
id: event.fetch('SecretId'),
|
22
|
+
secret: secret,
|
23
|
+
)
|
24
|
+
puts JSON.generate(plan: {step: req.step, token: req.token, id: req.id})
|
25
|
+
|
26
|
+
case req.step
|
27
|
+
when 'createSecret'
|
28
|
+
create_secret(req)
|
29
|
+
when 'setSecret'
|
30
|
+
set_secret(req)
|
31
|
+
when 'testSecret'
|
32
|
+
test_secret(req)
|
33
|
+
when 'finishSecret'
|
34
|
+
finish_secret(req)
|
35
|
+
else
|
36
|
+
raise "Unknown Step: #{req.step}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.prerequisite_check!(event)
|
41
|
+
secret = @secretsmanager.describe_secret(secret_id: event.fetch('SecretId'))
|
42
|
+
raise "secret #{secret.arn.inspect} have not enabled rotation" unless secret.rotation_enabled
|
43
|
+
stages = secret.version_ids_to_stages[event.fetch('ClientRequestToken')]
|
44
|
+
raise "Secret version #{event.fetch('ClientRequestToken').inspect} has no stage for secret #{secret.arn.inspect}" unless stages
|
45
|
+
raise "Secret version #{event.fetch('ClientRequestToken').inspect} is on AWSCURRENT for secret #{secret.arn.inspect}" if stages.include?('AWSCURRENT') && !stages.include?('AWSPENDING')
|
46
|
+
raise "Secret version #{event.fetch('ClientRequestToken').inspect} is not on AWSPENDING for secret #{secret.arn.inspect}" unless stages.include?('AWSPENDING')
|
47
|
+
secret
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.create_secret(req)
|
51
|
+
current = begin
|
52
|
+
@secretsmanager.get_secret_value(secret_id: req.id, version_stage: 'AWSCURRENT')
|
53
|
+
rescue ::Aws::SecretsManager::Errors::ResourceNotFoundException
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
puts "createSecret: current version is: #{current.version_id} @ #{current.arn}" if current
|
57
|
+
|
58
|
+
begin
|
59
|
+
@secretsmanager.get_secret_value(secret_id: req.id, version_id: req.token, version_stage: 'AWSPENDING')
|
60
|
+
rescue ::Aws::SecretsManager::Errors::ResourceNotFoundException
|
61
|
+
puts "createSecret: generating for #{req.token} @ #{req.id}"
|
62
|
+
|
63
|
+
@secretsmanager.put_secret_value(
|
64
|
+
secret_id: req.id,
|
65
|
+
client_request_token: req.token,
|
66
|
+
secret_string: generate_secret(req, current),
|
67
|
+
)
|
68
|
+
else
|
69
|
+
puts "createSecret: do nothing for #{req.token} @ #{req.id}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.generate_secret(req, _current)
|
74
|
+
param = JSON.parse(req.secret.tags.find { |t| t.name == ENV.fetch('HIMARI_KEYGEN_PARAM_TAG_KEY', 'HimariKey') }&.value || ENV.fetch('HIMARI_KEYGEN_PARAM_DEFAULT', '{"kty": "rsa", "len": 2048}'), symbolize_names: true)
|
75
|
+
puts "createSecret: generate_secret with #{param.inspect}"
|
76
|
+
|
77
|
+
case param.fetch(:kty, 'rsa').downcase
|
78
|
+
when 'rsa'
|
79
|
+
rsa = OpenSSL::PKey::RSA.generate(param.fetch(:len, 2048).to_i)
|
80
|
+
JSON.pretty_generate({kind: 'himari.signing_key', kty: 'rsa', rsa: {pem: rsa.to_pem}})
|
81
|
+
when 'ec'
|
82
|
+
curve = case param.fetch(:len, 256).to_i
|
83
|
+
when 256; 'prime256v1'
|
84
|
+
when 384; 'secp384r1'
|
85
|
+
when 521; 'secp521r1'
|
86
|
+
else
|
87
|
+
raise ArgumentError, "unknown len: #{param.inspect}"
|
88
|
+
end
|
89
|
+
ec = OpenSSL::PKey::EC.generate(curve)
|
90
|
+
JSON.pretty_generate({kind: 'himari.signing_key', kty: 'ec', ec: {pem: ec.to_pem}})
|
91
|
+
else
|
92
|
+
raise ArgumentError, "unknown kty: #{param.inspect}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.set_secret(req)
|
97
|
+
_check = @secretsmanager.get_secret_value(secret_id: req.id, version_id: req.token, version_stage: 'AWSPENDING')
|
98
|
+
puts "setSecret: do nothing for #{req.token} @ #{req.id}"
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.test_secret(req)
|
102
|
+
_check = @secretsmanager.get_secret_value(secret_id: req.id, version_id: req.token, version_stage: 'AWSPENDING')
|
103
|
+
puts "testSecret: do nothing for #{req.token} @ #{req.id}"
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.finish_secret(req)
|
107
|
+
current_version = req.secret.version_ids_to_stages.find { |k,v| v.include?('AWSCURRENT') }.first
|
108
|
+
if current_version == req.token
|
109
|
+
puts "finishSecret: #{current_version} on #{req.id} is on AWSCURRENT, do nothing"
|
110
|
+
return
|
111
|
+
end
|
112
|
+
|
113
|
+
puts "finishSecret: update_secret_version_stage AWSCURRENT to #{req.token} from #{current_version} for #{req.id}"
|
114
|
+
@secretsmanager.update_secret_version_stage(
|
115
|
+
secret_id: req.id,
|
116
|
+
version_stage: 'AWSCURRENT',
|
117
|
+
move_to_version_id: req.token,
|
118
|
+
remove_from_version_id: current_version,
|
119
|
+
)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/lib/himari/aws.rb
ADDED
data/lib/himari-aws.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'himari/aws'
|
data/sig/himari/aws.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: himari-aws
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sorah Fukumori
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: himari
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-secretsmanager
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: aws-sdk-dynamodb
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- her@sorah.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".rspec"
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/himari-aws.rb
|
69
|
+
- lib/himari/aws.rb
|
70
|
+
- lib/himari/aws/dynamodb_storage.rb
|
71
|
+
- lib/himari/aws/secretsmanager_signing_key_provider.rb
|
72
|
+
- lib/himari/aws/secretsmanager_signing_key_rotation_handler.rb
|
73
|
+
- lib/himari/aws/version.rb
|
74
|
+
- sig/himari/aws.rbs
|
75
|
+
homepage: https://github.com/sorah/himari
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata:
|
79
|
+
homepage_uri: https://github.com/sorah/himari
|
80
|
+
source_code_uri: https://github.com/sorah/himari
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.7.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.4.0.dev
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: AWS related plugins for Himari
|
100
|
+
test_files: []
|