hocho-jwt 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/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +62 -0
- data/LICENSE.txt +21 -0
- data/README.md +100 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hocho-jwt.gemspec +28 -0
- data/lib/hocho/jwt.rb +3 -0
- data/lib/hocho/jwt/version.rb +5 -0
- data/lib/hocho/property_providers/jwt.rb +133 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5016c6937fbb5f6380d20ac14aa17818aba2331737a314202cc18d85de983e4b
|
4
|
+
data.tar.gz: a6e44884f54eb2592c89ea66755629de34272a3adee65bb49a9ba3011547492f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87335b218e85acf9679aa9812e342c90a053773a53154f4514dc8124cdc1a4b87eb8a63299931764e2a4f19f14d8b566b3fa744aab393e41a88a8ddc149cfcc5
|
7
|
+
data.tar.gz: 984a4fe10b5bba4baca30e3d75c9e91bd3459f52a2d9e009ab66ecc5e39468d7266397d991d7d1ff268ef8f26fa18f91e70c8b1b415f728d98d10d50688cb5f7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hocho-jwt (0.1.0)
|
5
|
+
hocho
|
6
|
+
jwt
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ansi (1.5.0)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
hashie (4.1.0)
|
14
|
+
hocho (0.3.4)
|
15
|
+
hashie
|
16
|
+
itamae
|
17
|
+
net-ssh (>= 4.1.0)
|
18
|
+
thor
|
19
|
+
itamae (1.10.7)
|
20
|
+
ansi
|
21
|
+
hashie
|
22
|
+
schash (~> 0.1.0)
|
23
|
+
specinfra (>= 2.64.0, < 3.0.0)
|
24
|
+
thor
|
25
|
+
jwt (2.2.1)
|
26
|
+
net-scp (3.0.0)
|
27
|
+
net-ssh (>= 2.6.5, < 7.0.0)
|
28
|
+
net-ssh (6.0.2)
|
29
|
+
net-telnet (0.1.1)
|
30
|
+
rake (12.3.3)
|
31
|
+
rspec (3.9.0)
|
32
|
+
rspec-core (~> 3.9.0)
|
33
|
+
rspec-expectations (~> 3.9.0)
|
34
|
+
rspec-mocks (~> 3.9.0)
|
35
|
+
rspec-core (3.9.2)
|
36
|
+
rspec-support (~> 3.9.3)
|
37
|
+
rspec-expectations (3.9.2)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-mocks (3.9.1)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.9.0)
|
43
|
+
rspec-support (3.9.3)
|
44
|
+
schash (0.1.2)
|
45
|
+
sfl (2.3)
|
46
|
+
specinfra (2.82.16)
|
47
|
+
net-scp
|
48
|
+
net-ssh (>= 2.7)
|
49
|
+
net-telnet (= 0.1.1)
|
50
|
+
sfl
|
51
|
+
thor (1.0.1)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
hocho-jwt!
|
58
|
+
rake (~> 12.0)
|
59
|
+
rspec (~> 3.0)
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 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,100 @@
|
|
1
|
+
# hocho-jwt: Pass JWT to servers on `hocho apply`
|
2
|
+
|
3
|
+
[hocho](https://github.com/sorah/hocho) plugin to issue and sign JWT (JSON Web Token) and pass it to a host as a node attribute on `hocho apply`.
|
4
|
+
|
5
|
+
## Use cases
|
6
|
+
|
7
|
+
- Use with step-ca ([smallstep/certificates](https://github.com/smallstep/certificates)): allow servers to get certificates from step-ca during initial provision with hocho
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'hocho-jwt'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```yaml
|
20
|
+
# hocho.yml
|
21
|
+
property_providers:
|
22
|
+
- add_default:
|
23
|
+
properties:
|
24
|
+
hocho_jwt:
|
25
|
+
## JWT will be issued when host.properties.jwt.issue is present
|
26
|
+
issue: false
|
27
|
+
|
28
|
+
## Lifetime in seconds
|
29
|
+
duration: 120
|
30
|
+
|
31
|
+
## Claims
|
32
|
+
claims:
|
33
|
+
iss: hocho
|
34
|
+
|
35
|
+
## Set false to skip when issue=true but no signing key is present.
|
36
|
+
fail_when_no_signing_key: true
|
37
|
+
- jwt:
|
38
|
+
algorithm: ES256
|
39
|
+
|
40
|
+
## Signing Private Key
|
41
|
+
# EC256: openssl ecparam -name prime256v1 -genkey -noout -out key.pem
|
42
|
+
# RS256: openssl genrsa -noout -out key.pem 2048
|
43
|
+
signing_key:
|
44
|
+
## Key ID
|
45
|
+
# kid_string: key-id
|
46
|
+
# kid_file: path/to/kid
|
47
|
+
# kid_env: HOCHO_JWT_KID
|
48
|
+
## String to PEM or Base64 encoded DER
|
49
|
+
# pem_string: "..."
|
50
|
+
# pem_file: /path/to/jwk
|
51
|
+
# pem_env: HOCHO_JWT_KEY
|
52
|
+
|
53
|
+
## Templates are rendered using ERB, and `host` (Hocho::Host) is given.
|
54
|
+
## Invalid DNS names will be removed.
|
55
|
+
sub_template: "<%= host.name %>"
|
56
|
+
```
|
57
|
+
|
58
|
+
```yaml
|
59
|
+
# hocho host.yml
|
60
|
+
myhost.example.org:
|
61
|
+
properties:
|
62
|
+
hocho_jwt:
|
63
|
+
issue: true
|
64
|
+
claims:
|
65
|
+
# this overrides sub_template
|
66
|
+
# sub: "static-subject.example.org"
|
67
|
+
aud: 'audience'
|
68
|
+
customclaim: test
|
69
|
+
customclaimarray: ['test']
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# itamae recipe
|
75
|
+
|
76
|
+
file "/etc/jwt.txt" do
|
77
|
+
content "#{node[:hocho_jwt][:token]}\n"
|
78
|
+
owner 'root'
|
79
|
+
group 'root'
|
80
|
+
mode '0640'
|
81
|
+
end
|
82
|
+
|
83
|
+
execute "do-something-with-token"
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
## Development
|
88
|
+
|
89
|
+
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.
|
90
|
+
|
91
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hocho-jwt.
|
96
|
+
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hocho/jwt"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/hocho-jwt.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/hocho/jwt/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hocho-jwt"
|
5
|
+
spec.version = Hocho::Jwt::VERSION
|
6
|
+
spec.authors = ["Sorah Fukumori"]
|
7
|
+
spec.email = ["her@sorah.jp"]
|
8
|
+
|
9
|
+
spec.summary = %q{Pass JWT to servers using hocho}
|
10
|
+
spec.homepage = "https://github.com/sorah/hocho-jwt"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency 'jwt'
|
27
|
+
spec.add_dependency 'hocho'
|
28
|
+
end
|
data/lib/hocho/jwt.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'jwt'
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
require 'hocho/property_providers/base'
|
6
|
+
|
7
|
+
module Hocho
|
8
|
+
module PropertyProviders
|
9
|
+
class Jwt < Base
|
10
|
+
Token = Struct.new(:payload, :headers, :signing_key, :algorithm) do
|
11
|
+
def to_s
|
12
|
+
JWT.encode(
|
13
|
+
payload,
|
14
|
+
signing_key,
|
15
|
+
algorithm,
|
16
|
+
headers,
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(algorithm:, signing_key:, sub_template:, target: :hocho_jwt)
|
22
|
+
@algorithm = algorithm
|
23
|
+
@signing_key, @kid = load_key(**(signing_key || {}))
|
24
|
+
@sub_template = Class.new(Template).tap { |t| t.erb = ERB.new(sub_template) }
|
25
|
+
@target = target
|
26
|
+
end
|
27
|
+
|
28
|
+
def determine(host)
|
29
|
+
request = Request.new(**{
|
30
|
+
issue: false,
|
31
|
+
claims: {},
|
32
|
+
fail_when_no_signing_key: true,
|
33
|
+
}.merge(host.properties[@target] || {}))
|
34
|
+
|
35
|
+
return unless host.properties.dig(@target, :issue)
|
36
|
+
|
37
|
+
unless @signing_key
|
38
|
+
if request.fail_when_no_signing_key
|
39
|
+
raise ArgumentError, "cannot issue JWT key, no signing key is present"
|
40
|
+
else
|
41
|
+
return
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
headers = {}
|
46
|
+
headers[:kid] = @kid if @kid
|
47
|
+
|
48
|
+
payload = generate_payload(request, host)
|
49
|
+
host.attributes[@target] = {
|
50
|
+
payload: payload,
|
51
|
+
token: Token.new(
|
52
|
+
payload,
|
53
|
+
headers,
|
54
|
+
@signing_key,
|
55
|
+
@algorithm,
|
56
|
+
),
|
57
|
+
}
|
58
|
+
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
class Template
|
65
|
+
def self.erb; @erb; end
|
66
|
+
def self.erb=(x); @erb = x; end
|
67
|
+
|
68
|
+
def initialize(host)
|
69
|
+
@host = host
|
70
|
+
end
|
71
|
+
|
72
|
+
attr_reader :host
|
73
|
+
|
74
|
+
def result()
|
75
|
+
self.class.erb.result(binding)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Request = Struct.new(:issue, :duration, :claims, :fail_when_no_signing_key, keyword_init: true)
|
80
|
+
|
81
|
+
def generate_payload(request, host)
|
82
|
+
now = Time.now
|
83
|
+
data = {
|
84
|
+
sub: @sub_template.new(host).result(),
|
85
|
+
}
|
86
|
+
data[:iat] = now.to_i
|
87
|
+
if request.duration
|
88
|
+
data[:nbf] = now.to_i
|
89
|
+
data[:exp] = (now + request.duration).to_i
|
90
|
+
end
|
91
|
+
data.merge(request.claims)
|
92
|
+
end
|
93
|
+
|
94
|
+
def load_key(pem_string: nil, pem_file: nil, pem_env: nil, kid_string: nil, kid_file: nil, kid_env: nil)
|
95
|
+
pem = case
|
96
|
+
when pem_string
|
97
|
+
pem_string.to_s
|
98
|
+
when pem_file
|
99
|
+
File.read(pem_string) rescue nil
|
100
|
+
when pem_env
|
101
|
+
ENV[pem_env].yield_self do |e|
|
102
|
+
if e.start_with?('-----')
|
103
|
+
e
|
104
|
+
else
|
105
|
+
e.unpack1('m*')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
return nil unless pem
|
110
|
+
|
111
|
+
kid = case
|
112
|
+
when kid_string
|
113
|
+
kid_string.to_s
|
114
|
+
when kid_file
|
115
|
+
File.read(kid_string).chomp
|
116
|
+
when kid_env
|
117
|
+
ENV.fetch(kid_env)
|
118
|
+
end
|
119
|
+
|
120
|
+
key = case @algorithm
|
121
|
+
when /^ES/
|
122
|
+
OpenSSL::PKey::EC.new(pem, '')
|
123
|
+
when /^RS/
|
124
|
+
OpenSSL::PKey::RSA.new(pem, '')
|
125
|
+
else
|
126
|
+
raise ArgumentError, "unsupported cipher algorithm"
|
127
|
+
end
|
128
|
+
|
129
|
+
[key, kid]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hocho-jwt
|
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: 2020-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
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: hocho
|
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
|
+
description:
|
42
|
+
email:
|
43
|
+
- her@sorah.jp
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- hocho-jwt.gemspec
|
58
|
+
- lib/hocho/jwt.rb
|
59
|
+
- lib/hocho/jwt/version.rb
|
60
|
+
- lib/hocho/property_providers/jwt.rb
|
61
|
+
homepage: https://github.com/sorah/hocho-jwt
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
homepage_uri: https://github.com/sorah/hocho-jwt
|
66
|
+
source_code_uri: https://github.com/sorah/hocho-jwt
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.1.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Pass JWT to servers using hocho
|
86
|
+
test_files: []
|