etcdv3 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e8425d9e034d0a1bab9cc37a5250694854807e8
4
- data.tar.gz: b49869fe7b0fa100faccc29dd9d06806753c6a5d
3
+ metadata.gz: 6a62664451e058e0837e78b5ea583944532f596e
4
+ data.tar.gz: df7954ad160f76a260d297a8c13357f50ef15474
5
5
  SHA512:
6
- metadata.gz: '073189ecdbaa0c0719731731a4d2d05a0db0271855e7777f1c35d775d1196ea57554aa2cd8eaca7951e1a329dcf476d80f2024ee1aabe0ae540afe5ef024f5f7'
7
- data.tar.gz: 0bdc23ddadaebb5827e3a241e5f7d7ec4a536d32196f1c1e64bbc3991433cd81fdbd05f664ea5cd1e776bede027b65da78754f1f1e8604a40295e63cdc83b920
6
+ metadata.gz: af31ea39606da03fed56cbfb723d1958feaec6507c1f13ee60dba9ab9c870bcdeaead2cec21ea79ea373a3eb9a4ece179684a3a9688ab86d659cfe9918b4fc8e
7
+ data.tar.gz: 1037364a45050545ab05b6dca88661fe9ecd6d6824a29ef9fcfe53abcd8a55044550f89a4e407708c558fbd57fe865735adc78349f57001238c1ae6527e185de
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  language: ruby
2
- python: 2.4
2
+ rvm:
3
+ - 2.4.0
4
+ - 2.2
5
+ - 2.0
3
6
 
4
7
  env:
5
8
  global:
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Ruby client for Etcd V3
4
4
 
5
- **WARNING: This is very much a work in progress and should be considered unstable.**
5
+ **Note: This is under active development and is not feature complete**
6
6
 
7
7
  ## Getting Started
8
8
 
@@ -11,7 +11,7 @@ To install etcdv3, run the following command:
11
11
  gem install etcdv3
12
12
  ```
13
13
 
14
- You can connect to Etcd by instantiating the Etcd class:
14
+ **Establishing a connection**
15
15
 
16
16
  ```
17
17
  require 'etcdv3'
@@ -34,28 +34,53 @@ conn = Etcd.new(url: 'https://hostname:port', user: "gary", password: "secret")
34
34
  # Put
35
35
  conn.put("my", "value")
36
36
 
37
- # Range
38
- conn.range("my")
37
+ # Get
38
+ conn.get("my")
39
39
 
40
- **User Managment**
40
+ # Get Key Range
41
+ conn.get('my', 'myyyy')
41
42
 
42
- # Add User
43
- conn.add_user('admin', 'secret')
43
+ **User Management**
44
+ ```
45
+ # Add User
46
+ conn.add_user('admin', 'secret')
47
+
48
+ # Delete User
49
+ conn.delete_user('admin')
44
50
 
45
- # Delete User
46
- conn.delete_user('admin')
51
+ # List users
52
+ conn.user_list
53
+ ```
47
54
 
48
- # List users
49
- conn.user_list
55
+ **Role Management**
56
+ ```
57
+ # Add Role
58
+ conn.add_role('rolename', 'readwrite', 'a', 'Z')
50
59
 
51
- # Add Role
52
- conn.add_role('rolename', 'readwrite', 'a', 'Z')
60
+ # Delete Role
61
+ conn.delete_role('rolename')
53
62
 
54
- # Enable Authentication
55
- conn.add_user('root', "secret")
56
- conn.grant_role_to_user('root', 'root')
57
- conn.enable_auth
63
+ # List Roles
64
+ conn.role_list
65
+ ```
66
+
67
+ **Authentication Management**
68
+ ```
69
+ # Configure a root user
70
+ conn.add_user('root', 'mysecretpassword')
58
71
 
59
- # Disable Authentication
60
- conn = Etcd.new(url: 'http://127.0.0.1:2379', user: 'root', password: 'secret')
61
- conn.disable_auth
72
+ # Grant root user the root role
73
+ conn.grant_role_to_user('root', 'root')
74
+
75
+ # Enable Authentication
76
+ conn.enable_auth
77
+ ```
78
+ After you enable authentication, you must authenticate.
79
+ ```
80
+ # This will generate and assign an auth token that will be used in future requests.
81
+ conn.authenticate('root', 'mysecretpassword')
82
+ ```
83
+ Disabling auth will clear the auth token and all previously attached user information
84
+ ```
85
+ conn.disable_auth
86
+ ```
data/lib/etcdv3/auth.rb CHANGED
@@ -8,8 +8,9 @@ class Etcd
8
8
  'readwrite' => Authpb::Permission::Type::READWRITE
9
9
  }
10
10
 
11
- def initialize(hostname, port, credentials)
11
+ def initialize(hostname, port, credentials, metadata = {})
12
12
  @stub = Etcdserverpb::Auth::Stub.new("#{hostname}:#{port}", credentials)
13
+ @metadata = metadata
13
14
  end
14
15
 
15
16
  def generate_token(user, password)
@@ -19,69 +20,57 @@ class Etcd
19
20
  response.token
20
21
  rescue GRPC::FailedPrecondition => exception
21
22
  puts exception.message
23
+ false
22
24
  end
23
25
 
24
- def user_list(metadata = {})
25
- @stub.user_list(Authpb::User.new, metadata: metadata).users
26
- rescue GRPC::FailedPrecondition => exception
27
- puts exception.message
26
+ def user_list
27
+ @stub.user_list(Authpb::User.new, metadata: @metadata).users
28
28
  end
29
29
 
30
- def add_user(user, password, metadata = {})
30
+ def add_user(user, password)
31
31
  @stub.user_add(
32
- Authpb::User.new(name: user, password: password), metadata: metadata
32
+ Authpb::User.new(name: user, password: password), metadata: @metadata
33
33
  )
34
- rescue GRPC::FailedPrecondition => exception
35
- puts exception.message
36
34
  end
37
35
 
38
- def delete_user(user, metadata = {})
39
- @stub.user_delete(Authpb::User.new(name: user), metadata: metadata)
40
- rescue GRPC::FailedPrecondition => exception
41
- puts exception.message
36
+ def delete_user(user)
37
+ @stub.user_delete(Authpb::User.new(name: user))
42
38
  end
43
39
 
44
- def add_role(name, permission, key, range_end, metadata = {})
40
+ def add_role(name, permission, key, range_end)
45
41
  permission = Authpb::Permission.new(
46
42
  permType: Etcd::Auth::PERMISSIONS[permission], key: key, range_end: range_end
47
43
  )
48
44
  @stub.role_add(
49
45
  Authpb::Role.new(name: name, keyPermission: [permission]),
50
- metadata: metadata
46
+ metadata: @metadata
51
47
  )
52
- rescue GRPC::FailedPrecondition => exception
53
- puts exception.message
54
48
  end
55
49
 
56
- def delete_role(name, metadata = {})
57
- @stub.role_delete(Authpb::Role.new(name: name), metadata: metadata)
58
- rescue GRPC::FailedPrecondition => exception
59
- puts exception.message
50
+ def delete_role(name)
51
+ @stub.role_delete(Authpb::Role.new(name: name), metadata: @metadata)
60
52
  end
61
53
 
62
- def grant_role_to_user(user, role, metadata = {})
54
+ def grant_role_to_user(user, role)
63
55
  request = Etcdserverpb::AuthUserGrantRoleRequest.new(user: user, role: role)
64
- @stub.user_grant_role(request)
65
- rescue GRPC::FailedPrecondition => exception
66
- puts exception.message
56
+ @stub.user_grant_role(request, metadata: @metadata)
67
57
  end
68
58
 
69
- def role_list(metadata = {})
70
- @stub.role_list(Authpb::Role.new, metadata: metadata)
71
- rescue GRPC::FailedPrecondition => exception
72
- puts exception.message
59
+ def revoke_role_from_user(user, role)
60
+ request = Etcdserverpb::AuthUserRevokeRoleRequest.new(name: user, role: role)
61
+ @stub.user_revoke_role(request, metadata: @metadata)
62
+ end
63
+
64
+ def role_list
65
+ @stub.role_list(Authpb::Role.new, metadata: @metadata)
73
66
  end
74
67
 
75
68
  def enable_auth
76
69
  @stub.auth_enable(Authpb::User.new)
77
- rescue GRPC::FailedPrecondition => exception
78
- puts exception.message
79
70
  end
80
71
 
81
- def disable_auth(metadata = {})
82
- @stub.auth_disable(Authpb::User.new, metadata: metadata)
83
- rescue GRPC::FailedPrecondition => exception
84
- puts exception.message
72
+ def disable_auth
73
+ @stub.auth_disable(Authpb::User.new, metadata: @metadata)
85
74
  end
86
75
 
87
76
  end
data/lib/etcdv3/kv.rb CHANGED
@@ -1,19 +1,19 @@
1
1
 
2
2
  class Etcd
3
3
  class KV
4
- def initialize(hostname, port, credentials)
4
+ def initialize(hostname, port, credentials, metadata={})
5
5
  @stub = Etcdserverpb::KV::Stub.new("#{hostname}:#{port}", credentials)
6
+ @metadata = metadata
6
7
  end
7
8
 
8
- def put(key, value, metadata = {})
9
+ def put(key, value)
9
10
  kv = Etcdserverpb::PutRequest.new(key: key, value: value)
10
- @stub.put(kv, metadata: metadata)
11
+ @stub.put(kv, metadata: @metadata)
11
12
  end
12
13
 
13
- def range(key, range_end, metadata = {})
14
+ def get(key, range_end="")
14
15
  kv = Etcdserverpb::RangeRequest.new(key: key, range_end: range_end)
15
- result = @stub.range(kv, metadata: metadata)
16
- result.kvs
16
+ @stub.range(kv, metadata: @metadata).kvs
17
17
  end
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  class Etcd
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/etcdv3.rb CHANGED
@@ -46,39 +46,56 @@ class Etcd
46
46
  end
47
47
 
48
48
  def put(key, value)
49
- kv.put(key, value, @metadata)
49
+ kv.put(key, value)
50
50
  end
51
51
 
52
- def range(key, range_end)
53
- kv.range(key, range_end, @metadata)
52
+ def get(key, range_end='')
53
+ kv.get(key, range_end)
54
54
  end
55
55
 
56
56
  def add_user(user, password)
57
- auth.add_user(user, password, @metadata)
57
+ auth.add_user(user, password)
58
58
  end
59
59
 
60
60
  def delete_user(user)
61
- auth.delete_user(user, @metadata)
61
+ auth.delete_user(user)
62
62
  end
63
63
 
64
64
  def user_list
65
- auth.user_list(@metadata)
65
+ auth.user_list
66
+ end
67
+
68
+ def authenticate(user, password)
69
+ token = auth.generate_token(user, password)
70
+ if token
71
+ @metadata[:token] = token
72
+ @options[:user] = user
73
+ @options[:password] = password
74
+ return true
75
+ end
76
+ rescue GRPC::InvalidArgument => exception
77
+ print exception.message
78
+ return false
66
79
  end
67
80
 
68
81
  def role_list
69
- auth.role_list(@metadata)
82
+ auth.role_list
70
83
  end
71
84
 
72
85
  def add_role(name, permission, key, range_end='')
73
- auth.add_role(name, permission, key, range_end, @metadata)
86
+ auth.add_role(name, permission, key, range_end)
74
87
  end
75
88
 
76
89
  def delete_role(name)
77
- auth.delete_role(name, @metadata)
90
+ auth.delete_role(name)
78
91
  end
79
92
 
80
93
  def grant_role_to_user(user, role)
81
- auth.grant_role_to_user(user, role, @metadata)
94
+ auth.grant_role_to_user(user, role)
95
+ end
96
+
97
+ def revoke_role_from_user(user, role)
98
+ auth.revoke_role_from_user(user, role)
82
99
  end
83
100
 
84
101
  def enable_auth
@@ -86,17 +103,23 @@ class Etcd
86
103
  end
87
104
 
88
105
  def disable_auth
89
- auth.disable_auth(@metadata)
106
+ response = auth.disable_auth
107
+ if response
108
+ @metadata.delete(:token)
109
+ @options[:user] = nil
110
+ @options[:password] = nil
111
+ end
112
+ response
90
113
  end
91
114
 
92
115
  private
93
116
 
94
117
  def auth
95
- Etcd::Auth.new(hostname, port, @credentials)
118
+ Etcd::Auth.new(hostname, port, @credentials, @metadata)
96
119
  end
97
120
 
98
121
  def kv
99
- Etcd::KV.new(hostname, port, @credentials)
122
+ Etcd::KV.new(hostname, port, @credentials, @metadata)
100
123
  end
101
124
 
102
125
  def resolve_credentials
@@ -2,13 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe Etcd::Auth do
4
4
 
5
- let(:stub) do
6
- Etcd::Auth.new('127.0.0.1', 2379, :this_channel_is_insecure)
5
+ let(:conn) do
6
+ Etcd.new(url: 'http://127.0.0.1:2379')
7
7
  end
8
8
 
9
9
  describe '#add_user' do
10
10
  it 'returns AuthUserAddResponse' do
11
- expect(stub.add_user("boom", 'test')).to \
11
+ expect(conn.add_user("boom", 'test')).to \
12
12
  be_an_instance_of(Etcdserverpb::AuthUserAddResponse)
13
13
  end
14
14
  end
@@ -16,88 +16,128 @@ describe Etcd::Auth do
16
16
  describe '#user_list' do
17
17
 
18
18
  it 'returns Protobuf' do
19
- expect(stub.user_list).to \
19
+ expect(conn.user_list).to \
20
20
  be_an_instance_of(Google::Protobuf::RepeatedField)
21
21
  end
22
22
 
23
23
  it 'returns listme user' do
24
- stub.add_user('listme', 'test')
25
- expect(stub.user_list).to include('listme')
24
+ conn.add_user('listme', 'test')
25
+ expect(conn.user_list).to include('listme')
26
26
  end
27
27
  end
28
28
 
29
29
  describe '#delete_user' do
30
30
  before do
31
- stub.add_user('testuser', 'test')
31
+ conn.add_user('testuser', 'test')
32
32
  end
33
33
  it 'returns AuthUserDeleteResponse' do
34
- expect(stub.delete_user('testuser')).to \
34
+ expect(conn.delete_user('testuser')).to \
35
35
  be_an_instance_of(Etcdserverpb::AuthUserDeleteResponse)
36
36
  end
37
37
  end
38
38
 
39
39
  describe '#grant_role_to_user' do
40
40
  before do
41
- stub.add_user('root', 'password')
41
+ conn.add_user('root', 'password')
42
42
  end
43
43
  after do
44
- stub.delete_user('root')
44
+ conn.delete_user('root')
45
45
  end
46
46
  it 'returns AuthUserGrantRoleResponse' do
47
- expect(stub.grant_role_to_user("root", 'root')).to \
47
+ expect(conn.grant_role_to_user("root", 'root')).to \
48
48
  be_an_instance_of(Etcdserverpb::AuthUserGrantRoleResponse)
49
49
  end
50
50
  end
51
51
 
52
+ describe '#revoke_role_from_user' do
53
+ before do
54
+ conn.add_user('test_user', 'password')
55
+ conn.grant_role_to_user('test_user', 'root')
56
+ end
57
+ after do
58
+ conn.delete_user('test_user')
59
+ end
60
+ it 'returns AuthUserGrantRoleResponse' do
61
+ expect(conn.revoke_role_from_user("test_user", 'root')).to \
62
+ be_an_instance_of(Etcdserverpb::AuthUserRevokeRoleResponse)
63
+ end
64
+ end
65
+
52
66
  describe '#add_role' do
53
67
  it 'returns AuthRoleAddResponse' do
54
- expect(stub.add_role('testRole', 'readwrite', 'a', 'Z')).to \
68
+ expect(conn.add_role('testRole', 'readwrite', 'a', 'Z')).to \
55
69
  be_an_instance_of(Etcdserverpb::AuthRoleAddResponse)
56
70
  end
57
71
  end
58
72
 
59
73
  describe '#add_delete' do
60
74
  it 'returns AuthRoleAddResponse' do
61
- expect(stub.delete_role('testRole')).to \
75
+ expect(conn.delete_role('testRole')).to \
62
76
  be_an_instance_of(Etcdserverpb::AuthRoleDeleteResponse)
63
77
  end
64
78
  end
65
79
 
66
80
  describe '#role_list' do
67
81
  it 'returns AuthRoleListResponse' do
68
- expect(stub.role_list).to \
82
+ expect(conn.role_list).to \
69
83
  be_an_instance_of(Etcdserverpb::AuthRoleListResponse)
70
84
  end
71
85
  end
72
86
 
73
87
  describe '#disable_auth' do
74
88
  before do
75
- stub.add_user('root', 'test')
76
- stub.grant_role_to_user('root', 'root')
77
- stub.enable_auth
89
+ conn.add_user('root', 'test')
90
+ conn.grant_role_to_user('root', 'root')
91
+ conn.enable_auth
92
+ conn.authenticate('root', 'test')
78
93
  end
79
94
  after do
80
- stub.delete_user('root')
95
+ conn.delete_user('root')
81
96
  end
82
97
  it 'returns AuthDisableResponse' do
83
- token = stub.generate_token('root', 'test')
84
- expect(stub.disable_auth(token: token)).to \
98
+ expect(conn.disable_auth).to \
85
99
  be_an_instance_of(Etcdserverpb::AuthDisableResponse)
86
100
  end
87
101
  end
88
102
 
89
103
  describe '#enable_auth' do
90
104
  before do
91
- stub.add_user('root', 'test')
92
- stub.grant_role_to_user('root', 'root')
105
+ conn.add_user('root', 'test')
106
+ conn.grant_role_to_user('root', 'root')
93
107
  end
94
108
  after do
95
- token = stub.generate_token('root', 'test')
96
- stub.disable_auth(token: token)
97
- stub.delete_user('root')
109
+ conn.authenticate('root', 'test')
110
+ conn.disable_auth
111
+ conn.delete_user('root')
98
112
  end
99
113
  it 'returns AuthEnableResponse' do
100
- expect(stub.enable_auth).to be_an_instance_of(Etcdserverpb::AuthEnableResponse)
114
+ expect(conn.enable_auth).to be_an_instance_of(Etcdserverpb::AuthEnableResponse)
115
+ end
116
+ end
117
+
118
+ describe "#authenticate" do
119
+ context "auth enabled" do
120
+ before do
121
+ conn.add_user('root', 'test')
122
+ conn.grant_role_to_user('root', 'root')
123
+ conn.enable_auth
124
+ conn.authenticate('root', 'test')
125
+ end
126
+ after do
127
+ conn.disable_auth
128
+ conn.delete_user('root')
129
+ end
130
+ it 'properly reconfigures token + user + password' do
131
+ expect(conn.token).to_not be_nil
132
+ expect(conn.user).to eq('root')
133
+ expect(conn.password).to eq('test')
134
+ end
135
+ end
136
+
137
+ context 'auth disabled' do
138
+ it 'returns false when authenticating with auth disabled' do
139
+ expect(conn.authenticate('root', 'root')).to eq(false)
140
+ end
101
141
  end
102
142
  end
103
143
  end
@@ -2,30 +2,28 @@ require 'spec_helper'
2
2
 
3
3
  describe Etcd::KV do
4
4
 
5
- let(:stub) do
6
- Etcd::KV.new('127.0.0.1', 2379, :this_channel_is_insecure)
5
+ let(:conn) do
6
+ Etcd.new(url: 'http://127.0.0.1:2379')
7
7
  end
8
8
 
9
9
  describe '#put' do
10
10
  it 'returns PutResponse' do
11
- expect(stub.put('test', 'test')).to \
11
+ expect(conn.put('test', 'test')).to \
12
12
  be_an_instance_of(Etcdserverpb::PutResponse)
13
13
  end
14
14
  end
15
15
 
16
- describe '#range' do
16
+ describe '#get' do
17
17
  before do
18
- stub.put('test', "zoom")
18
+ conn.put('test', "zoom")
19
19
  end
20
20
  it 'returns protobuf' do
21
- expect(stub.range('test', '')).to \
21
+ expect(conn.get('test')).to \
22
22
  be_an_instance_of(Google::Protobuf::RepeatedField)
23
23
  end
24
24
 
25
25
  it 'returns correct result' do
26
- expect(stub.range('test', '').first.key).to eq('test')
26
+ expect(conn.get('test').first.key).to eq('test')
27
27
  end
28
28
  end
29
-
30
-
31
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etcdv3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaun Davis
@@ -44,10 +44,8 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
- - ".codeclimate.yml"
48
47
  - ".gitignore"
49
48
  - ".rspec"
50
- - ".ruby-version"
51
49
  - ".travis.yml"
52
50
  - Gemfile
53
51
  - README.md
@@ -92,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
90
  version: '0'
93
91
  requirements: []
94
92
  rubyforge_project:
95
- rubygems_version: 2.6.11
93
+ rubygems_version: 2.6.8
96
94
  signing_key:
97
95
  specification_version: 4
98
96
  summary: A Etcd client library for Version 3
data/.codeclimate.yml DELETED
@@ -1,27 +0,0 @@
1
- engines:
2
- duplication:
3
- enabled: true
4
- config:
5
- languages:
6
- - ruby
7
- fixme:
8
- enabled: true
9
- rubocop:
10
- enabled: true
11
- ratings:
12
- paths:
13
- - "**.rb"
14
- exclude_paths:
15
- - spec/
16
- engines:
17
- rubocop:
18
- enabled: true
19
- duplication:
20
- enabled: true
21
- ratings:
22
- paths:
23
- - lib/**
24
- - "**.rb"
25
- exclude_paths:
26
- - "lib/etcdv3/protos/*"
27
- - "lib/etcdv3/etcdrpc/*"
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.4.1