x1-sat-support 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6968093245123be8d17c9bf51b06158a25671476
4
+ data.tar.gz: b1c6a1a1680279e96d7a0c40d9ccf72728d211fe
5
+ SHA512:
6
+ metadata.gz: 8944bbf84c9d9d75b47fb00417bab72c464d5f17f827988aba8252c511cd96453072b423d266c95c770986ff8b9d2ae630d26b9cd5e63c44393d4f2eccd025ee
7
+ data.tar.gz: bbb09b4609796f95ab28900691052936f286ed33f562d6b5bc156918da5b5982bbdd30695a0f13d3f92ecebf44bdbb7cf123d2a0a39593b9346284703361ccca
@@ -0,0 +1,32 @@
1
+ ##Install
2
+ Add to Gemfile:
3
+ gem 'x1-sat-support'
4
+
5
+ Generate config file by:
6
+ rake x1_sat_support:generate_config
7
+
8
+ Edit x1_sat_support.yml:
9
+ key_url: #url with public key
10
+ sat_token_url: #url with serviceAccessToken
11
+ path_to_store_keys: #path where public keys will be stored
12
+ keys_expiration_time: #lifetime for public key in seconds
13
+ numbers_of_keys_to_keep: #limit count public keys in storage (optional)
14
+
15
+ credentials:
16
+ principal: #header X-Codebig-Principal
17
+ client_id: #header X-Client-Id
18
+ client_secret: #header X-Client-Secret
19
+
20
+ Run rake task:
21
+ rake x1_sat_support:update_keys
22
+
23
+ #For manual use:
24
+ ```ruby
25
+ require 'x1-sat-support'
26
+
27
+ config = { "key_url"=>"...", "sat_token_url"=>"...", "path_to_store_keys"=>"...",
28
+ "keys_expiration_time"=>..., "numbers_of_keys_to_keep"=>...,
29
+ "credentials"=>{"principal"=>"...", "client_id"=>"...", "client_secret"=>"..."}}
30
+
31
+ X1SatSupport::Client.new(config).update_keys
32
+ ```
@@ -0,0 +1,14 @@
1
+ namespace :x1_sat_support do
2
+ desc 'Fetch public key from Sat service and store it'
3
+ task :update_keys do
4
+ config_file = File.exists?("config") ? "config/x1_sat_support.yml" : "x1_sat_support.yml"
5
+ raise "Config file is not found. Pleas run 'rake x1_sat_support:generate_config'" unless File.exists?(config_file)
6
+ X1SatSupport::Client.new(YAML.load(File.read(config_file))).update_keys
7
+ end
8
+
9
+ desc 'Generate config file'
10
+ task :generate_config do
11
+ config_file = File.exists?("config") ? "config/x1_sat_support.yml" : "x1_sat_support.yml"
12
+ File.write(config_file, File.read("#{File.expand_path("../../templates", __FILE__)}/x1_sat_support.yml"))
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ key_url:
2
+ sat_token_url:
3
+ path_to_store_keys:
4
+ keys_expiration_time:
5
+ numbers_of_keys_to_keep:
6
+
7
+ credentials:
8
+ principal:
9
+ client_id:
10
+ client_secret:
@@ -0,0 +1,5 @@
1
+ require File.expand_path File.join(File.dirname(__FILE__), 'x1_sat_support/client.rb')
2
+
3
+ module X1SatSupport
4
+ require 'x1_sat_support/railtie' if defined?(Rails)
5
+ end
@@ -0,0 +1,94 @@
1
+ require 'jwt'
2
+ require 'rest-client'
3
+
4
+ module X1SatSupport
5
+ class Client
6
+ #
7
+ # ==== Description
8
+ # This method initialize the client and client's variables
9
+ # ==== Parameters
10
+ # * <tt>config:</tt> - hash from config file that can be generated by 'rake x1_sat_support:generate_config'
11
+ #
12
+ def initialize(config)
13
+ @headers = {'X-Codebig-Principal' => config["credentials"]["principal"],
14
+ 'X-Client-Id' => config["credentials"]["client_id"],
15
+ 'X-Client-Secret' => config["credentials"]["client_secret"]}
16
+ @key_url = config["key_url"]
17
+ @sat_token_url = config["sat_token_url"]
18
+ @path_to_store_keys = config["path_to_store_keys"]
19
+ @keys_expiration_time = config["keys_expiration_time"]
20
+ @numbers_of_keys_to_keep = config["numbers_of_keys_to_keep"]
21
+ end
22
+ #
23
+ # ==== Description
24
+ # This method updates public keys
25
+ # It creates folder and write a file with public key there
26
+ #
27
+ def update_keys
28
+ unless @key_url && @sat_token_url && @path_to_store_keys && @keys_expiration_time
29
+ raise "Config is incorrect."
30
+ end
31
+ kid = get_kid
32
+ unless kid
33
+ raise "Key ID is not present"
34
+ end
35
+ key_name = kid + '.pub'
36
+ key = get_current_key
37
+ Dir.mkdir(@path_to_store_keys) unless File.exists?(@path_to_store_keys)
38
+ File.open("#{@path_to_store_keys}/#{key_name}", 'w') {|f| f.write(key)}
39
+ check_keys
40
+ end
41
+
42
+ private
43
+ #
44
+ # ==== Description
45
+ # This method makes get request that return encoded service access token and decode it
46
+ # ==== Return
47
+ # This method returns array of hashes with key id
48
+ #[{"jti"=>"<id>",
49
+ # "iss"=>"...",
50
+ # "sub"=>"...",
51
+ # "iat"=>...,
52
+ # "nbf"=>...,
53
+ # "exp"=>...,
54
+ # "version"=>"1.0",
55
+ # "allowedResources"=>{"allowedDeviceIds"=>["*"], "allowedPartners"=>["*"], "allowedServiceAccountIds"=>["*"], "allowedUserIds"=>["*"]},
56
+ # "capabilities"=>[],
57
+ # "aud"=>[]},
58
+ # {"kid"=>"...", "alg"=>"RS256"}]
59
+ #
60
+ def get_kid
61
+ encoded_token = RestClient.get(@sat_token_url, @headers).body["serviceAccessToken"]
62
+ token = JWT.decode encoded_token, nil, false
63
+ token.last["kid"]
64
+ end
65
+ #
66
+ # ==== Description
67
+ # This method makes get request that return public key
68
+ # ==== Return
69
+ # This method returns public key
70
+ #
71
+ def get_current_key
72
+ RestClient.get(@key_url, @headers).body
73
+ end
74
+ #
75
+ # ==== Description
76
+ # This method checks existed public keys.
77
+ # It sorts all keys *.pub in defined path by created time (path_to_store_keys in config). If file's lifetime is bigger then keys_expiration_time from the config, method will delete it.
78
+ # If numbers_of_keys_to_keep parameter is present in config, method removes files until files.count will be equal numbers_of_keys_to_keep.
79
+ # ==== Return
80
+ # This method returns public key
81
+ #
82
+ def check_keys
83
+ files = Dir[@path_to_store_keys + "/*.pub"].sort_by {|file| File.ctime(file)}
84
+ files.each do |file|
85
+ File.delete(files.delete(file)) if (Time.now - File.ctime(file)) > @keys_expiration_time
86
+ end
87
+ if @numbers_of_keys_to_keep
88
+ until @numbers_of_keys_to_keep >= files.count do
89
+ File.delete(files.shift)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,16 @@
1
+ require 'x1-sat-support'
2
+ require 'rails'
3
+
4
+ module X1SatSupport
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :x1_sat_support
7
+
8
+ rake_tasks do
9
+ load "tasks/x1_sat_support.rake"
10
+ end
11
+
12
+ generators do
13
+ require "generators/x1_sat_support/config/config_generator.rb"
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: x1-sat-support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Kharchenko
8
+ - Max Reznichenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.10'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.10'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rest-client
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: jwt
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 1.5.1
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 1.5.1
84
+ description: Gem fetches and stores public keys from X1 Sat service. Generator for
85
+ config and rake task are present
86
+ email:
87
+ - pkharchenko1990@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - README.md
93
+ - lib/tasks/x1_sat_support.rake
94
+ - lib/templates/x1_sat_support.yml
95
+ - lib/x1-sat-support.rb
96
+ - lib/x1_sat_support/client.rb
97
+ - lib/x1_sat_support/railtie.rb
98
+ homepage: http://rubygems.org/gems/x1-sat-support
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.9.3
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Fetcher for x1 sat public keys
122
+ test_files: []