conjur-cli 4.9.3 → 4.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,8 +24,6 @@ require 'etc'
24
24
  require 'socket'
25
25
 
26
26
  class Conjur::Command::Policy < Conjur::DSLCommand
27
- self.prefix = :policy
28
-
29
27
  class << self
30
28
  def default_collection_user
31
29
  Etc.getlogin
@@ -40,18 +38,20 @@ class Conjur::Command::Policy < Conjur::DSLCommand
40
38
  end
41
39
  end
42
40
 
43
- desc "Load a policy from Conjur DSL"
44
- long_desc <<-DESC
41
+ desc "Manage policies"
42
+ command :policy do |policy|
43
+ policy.desc "Load a policy from Conjur DSL"
44
+ policy.long_desc <<-DESC
45
45
  This method is EXPERIMENTAL and subject to change
46
46
 
47
47
  Loads a Conjur policy from DSL, applying particular conventions to the role and resource
48
- ids.
48
+ ids.
49
49
 
50
50
  The first path element of each id is the collection. Policies are separated into collections
51
51
  according to software development lifecycle. The default collection for a policy is $USER@$HOSTNAME,
52
52
  in other words, the username and hostname on which the policy is created. This is approriate for
53
- policy development and local testing. Once tested, policies can be created in more official
54
- environments such as ci, stage, and production.
53
+ policy development and local testing. Once tested, policies can be created in more official
54
+ environments such as ci, stage, and production.
55
55
 
56
56
  The second path element of each id is the policy name and version, following the convention
57
57
  policy-x.y.z, where x, y, and z are the semantic version of the policy.
@@ -60,25 +60,26 @@ Next, each policy creates a policy role and policy resource. The policy resource
60
60
  annotations on the policy. The policy role becomes the owner of the owned policy assets. The
61
61
  --as-group and --as-role options can be used to set the owner of the policy role. The default
62
62
  owner of the policy role is the logged-in user (you), as always.
63
- DESC
64
- arg_name "(policy-file | STDIN)"
65
- command :load do |c|
66
- acting_as_option(c)
67
-
68
- c.desc "Policy collection (default: #{default_collection_user}@#{default_collection_hostname})"
69
- c.arg_name "collection"
70
- c.flag [:collection]
71
-
72
- c.desc "Load context from this config file, and save it when finished. The file permissions will be 0600 by default."
73
- c.arg_name "context"
74
- c.flag [:c, :context]
75
-
76
- c.action do |global_options,options,args|
77
- collection = options[:collection] || default_collection_name
78
-
79
- run_script args, options do |runner, &block|
80
- runner.scope collection do
81
- block.call
63
+ DESC
64
+ policy.arg_name "(policy-file | STDIN)"
65
+ policy.command :load do |c|
66
+ acting_as_option(c)
67
+
68
+ c.desc "Policy collection (default: #{default_collection_user}@#{default_collection_hostname})"
69
+ c.arg_name "collection"
70
+ c.flag [:collection]
71
+
72
+ c.desc "Load context from this config file, and save it when finished. The file permissions will be 0600 by default."
73
+ c.arg_name "context"
74
+ c.flag [:c, :context]
75
+
76
+ c.action do |global_options,options,args|
77
+ collection = options[:collection] || default_collection_name
78
+
79
+ run_script args, options do |runner, &block|
80
+ runner.scope collection do
81
+ block.call
82
+ end
82
83
  end
83
84
  end
84
85
  end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright (C) 2013 Conjur Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the "Software"), to deal in
6
+ # the Software without restriction, including without limitation the rights to
7
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ # the Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ #
21
+
22
+ require 'conjur/cli'
23
+
24
+ class Conjur::Command::Pubkeys < Conjur::Command
25
+ desc "Public keys service operations"
26
+ command :pubkeys do |pubkeys|
27
+
28
+ pubkeys.desc "List public keys for the given user"
29
+ pubkeys.arg_name "username"
30
+ pubkeys.command :show do |c|
31
+ c.action do |global_options, options, args|
32
+ username = require_arg args, "username"
33
+ puts api.public_keys(username)
34
+ end
35
+ end
36
+
37
+ pubkeys.desc "List the names of a user's public keys"
38
+ pubkeys.arg_name "username"
39
+ pubkeys.command :names do |c|
40
+ c.action do |global_options, options, args|
41
+ username = require_arg args, "username"
42
+ api.public_keys(username)
43
+ .split("\n")
44
+ .map{|k| k.split(' ').last}
45
+ .sort.each{|n| puts n}
46
+ end
47
+ end
48
+
49
+ pubkeys.desc "Add a public key for a user"
50
+ pubkeys.arg_name "username key"
51
+ pubkeys.command :add do |c|
52
+ c.action do |global_options, options, args|
53
+ username = require_arg args, "username"
54
+ if key = args.shift
55
+ if /^@(.+)$/ =~ key
56
+ key = File.read(File.expand_path($1))
57
+ end
58
+ else
59
+ key = STDIN.read.strip
60
+ end
61
+ api.add_public_key username, key
62
+ puts "Public key '#{key.split(' ').last}' added"
63
+ end
64
+ end
65
+
66
+ pubkeys.desc "Removes a public key for a user"
67
+ pubkeys.arg_name "username keyname"
68
+ pubkeys.command :delete do |c|
69
+ c.action do |global_options, options, args|
70
+ username = require_arg args, "username"
71
+ keyname = require_arg args, "keyname"
72
+ api.delete_public_key username, keyname
73
+ puts "Public key '#{keyname}' deleted"
74
+ end
75
+ end
76
+ end
77
+ end
@@ -18,160 +18,159 @@
18
18
  # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
19
  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
20
  #
21
- require 'conjur/authn'
22
- require 'conjur/resource'
23
- require 'conjur/command'
24
-
25
21
  class Conjur::Command::Resources < Conjur::Command
26
- self.prefix = :resource
27
-
28
- desc "Create a new resource"
29
- arg_name "resource-id"
30
- command :create do |c|
31
- acting_as_option(c)
32
-
33
- c.action do |global_options,options,args|
34
- id = full_resource_id( require_arg(args, "resource-id") )
35
- resource = api.resource(id)
36
-
37
- if ownerid = options.delete(:ownerid)
38
- options[:acting_as] = ownerid
39
- end
40
22
 
41
- resource.create(options)
42
- display resource.attributes
23
+ desc "Manage resources"
24
+ command :resource do |resource|
25
+
26
+ resource.desc "Create a new resource"
27
+ resource.arg_name "resource-id"
28
+ resource.command :create do |c|
29
+ acting_as_option(c)
30
+
31
+ c.action do |global_options,options,args|
32
+ id = full_resource_id( require_arg(args, "resource-id") )
33
+ resource = api.resource(id)
34
+
35
+ if ownerid = options.delete(:ownerid)
36
+ options[:acting_as] = ownerid
37
+ end
38
+
39
+ resource.create(options)
40
+ display resource.attributes
41
+ end
43
42
  end
44
- end
45
-
46
- desc "Show a resource"
47
- arg_name "resource-id"
48
- command :show do |c|
49
- c.action do |global_options,options,args|
50
- id = full_resource_id( require_arg(args, "resource-id") )
51
- display api.resource(id).attributes
43
+
44
+ resource.desc "Show a resource"
45
+ resource.arg_name "resource-id"
46
+ resource.command :show do |c|
47
+ c.action do |global_options,options,args|
48
+ id = full_resource_id( require_arg(args, "resource-id") )
49
+ display api.resource(id).attributes
50
+ end
52
51
  end
53
- end
54
-
55
- desc "Determines whether a resource exists"
56
- arg_name "resource-id"
57
- command :exists do |c|
58
- c.action do |global_options,options,args|
59
- id = full_resource_id( require_arg(args, "resource-id") )
60
- puts api.resource(id).exists?
52
+
53
+ resource.desc "Determines whether a resource exists"
54
+ resource.arg_name "resource-id"
55
+ resource.command :exists do |c|
56
+ c.action do |global_options,options,args|
57
+ id = full_resource_id( require_arg(args, "resource-id") )
58
+ puts api.resource(id).exists?
59
+ end
61
60
  end
62
- end
63
61
 
64
- desc "Give a privilege on a resource"
65
- arg_name "resource-id role privilege"
66
- command :permit do |c|
67
- c.action do |global_options,options,args|
68
- id = full_resource_id( require_arg(args, "resource-id") )
69
- role = require_arg(args, "role")
70
- privilege = require_arg(args, "privilege")
71
- api.resource(id).permit privilege, role
72
- puts "Permission granted"
62
+ resource.desc "Give a privilege on a resource"
63
+ resource.arg_name "resource-id role privilege"
64
+ resource.command :permit do |c|
65
+ c.action do |global_options,options,args|
66
+ id = full_resource_id( require_arg(args, "resource-id") )
67
+ role = require_arg(args, "role")
68
+ privilege = require_arg(args, "privilege")
69
+ api.resource(id).permit privilege, role
70
+ puts "Permission granted"
71
+ end
73
72
  end
74
- end
75
73
 
76
- desc "Deny a privilege on a resource"
77
- arg_name "resource-id role privilege"
78
- command :deny do |c|
79
- c.action do |global_options,options,args|
80
- id = full_resource_id( require_arg(args, "resource-id") )
81
- role = require_arg(args, "role")
82
- privilege = require_arg(args, "privilege")
83
- api.resource(id).deny privilege, role
84
- puts "Permission revoked"
74
+ resource.desc "Deny a privilege on a resource"
75
+ resource.arg_name "resource-id role privilege"
76
+ resource.command :deny do |c|
77
+ c.action do |global_options,options,args|
78
+ id = full_resource_id( require_arg(args, "resource-id") )
79
+ role = require_arg(args, "role")
80
+ privilege = require_arg(args, "privilege")
81
+ api.resource(id).deny privilege, role
82
+ puts "Permission revoked"
83
+ end
85
84
  end
86
- end
87
85
 
88
- desc "Check for a privilege on a resource"
89
- long_desc """
86
+ resource.desc "Check for a privilege on a resource"
87
+ resource.long_desc """
90
88
  By default, the privilege is checked for the logged-in user.
91
89
  Permission checks may be performed for other roles using the optional role argument.
92
90
  When the role argument is used, either the logged-in user must either own the specified
93
91
  resource or be an admin of the specified role (i.e. be granted the specified role with grant option).
94
92
  """
95
- arg_name "resource-id privilege"
96
- command :check do |c|
97
- c.desc "Role to check. By default, the current logged-in role is used"
98
- c.flag [:r,:role]
99
-
100
- c.action do |global_options,options,args|
101
- id = full_resource_id( require_arg(args, "resource-id") )
102
- privilege = args.shift or raise "Missing parameter: privilege"
103
- if role = options[:role]
104
- role = api.role(role)
105
- puts role.permitted? id, privilege
106
- else
107
- puts api.resource(id).permitted? privilege
93
+ resource.arg_name "resource-id privilege"
94
+ resource.command :check do |c|
95
+ c.desc "Role to check. By default, the current logged-in role is used"
96
+ c.flag [:r,:role]
97
+
98
+ c.action do |global_options,options,args|
99
+ id = full_resource_id( require_arg(args, "resource-id") )
100
+ privilege = args.shift or raise "Missing parameter: privilege"
101
+ if role = options[:role]
102
+ role = api.role(role)
103
+ puts role.permitted? id, privilege
104
+ else
105
+ puts api.resource(id).permitted? privilege
106
+ end
108
107
  end
109
108
  end
110
- end
111
109
 
112
- desc "Grant ownership on a resource to a new owner"
113
- arg_name "resource-id owner"
114
- command :give do |c|
115
- c.action do |global_options,options,args|
116
- id = full_resource_id( require_arg(args, "resource-id") )
117
- owner = require_arg(args, "owner")
118
- api.resource(id).give_to owner
119
- puts "Ownership granted"
110
+ resource.desc "Grant ownership on a resource to a new owner"
111
+ resource.arg_name "resource-id owner"
112
+ resource.command :give do |c|
113
+ c.action do |global_options,options,args|
114
+ id = full_resource_id( require_arg(args, "resource-id") )
115
+ owner = require_arg(args, "owner")
116
+ api.resource(id).give_to owner
117
+ puts "Ownership granted"
118
+ end
120
119
  end
121
- end
122
120
 
123
- desc "List roles with a specified permission on the resource"
124
- arg_name "resource-id permission"
125
- command :permitted_roles do |c|
126
- c.action do |global_options,options,args|
127
- id = full_resource_id( require_arg(args, "resource-id") )
128
- permission = require_arg(args, "permission")
129
- display api.resource(id).permitted_roles(permission)
121
+ resource.desc "List roles with a specified permission on the resource"
122
+ resource.arg_name "resource-id permission"
123
+ resource.command :permitted_roles do |c|
124
+ c.action do |global_options,options,args|
125
+ id = full_resource_id( require_arg(args, "resource-id") )
126
+ permission = require_arg(args, "permission")
127
+ display api.resource(id).permitted_roles(permission)
128
+ end
130
129
  end
131
- end
132
-
133
- desc "Set an annotation on a resource"
134
- arg_name "resource-id name value"
135
- command :annotate do |c|
136
- c.action do |global_options, options, args|
137
- id = full_resource_id require_arg(args, 'resource-id')
138
- name = require_arg args, 'name'
139
- value = require_arg args, 'value'
140
- api.resource(id).annotations[name] = value
141
- puts "Set annotation '#{name}' to '#{value}' for resource '#{id}'"
130
+
131
+ resource.desc "Set an annotation on a resource"
132
+ resource.arg_name "resource-id name value"
133
+ resource.command :annotate do |c|
134
+ c.action do |global_options, options, args|
135
+ id = full_resource_id require_arg(args, 'resource-id')
136
+ name = require_arg args, 'name'
137
+ value = require_arg args, 'value'
138
+ api.resource(id).annotations[name] = value
139
+ puts "Set annotation '#{name}' to '#{value}' for resource '#{id}'"
140
+ end
142
141
  end
143
- end
144
-
145
- desc "Show an annotation for a resource"
146
- arg_name "resource-id name"
147
- command :annotation do |c|
148
- c.action do |global_options, options, args|
149
- id = full_resource_id require_arg args, 'resource-id'
150
- name = require_arg args, 'name'
151
- value = api.resource(id).annotations[name]
152
- puts value unless value.nil?
142
+
143
+ resource.desc "Show an annotation for a resource"
144
+ resource.arg_name "resource-id name"
145
+ resource.command :annotation do |c|
146
+ c.action do |global_options, options, args|
147
+ id = full_resource_id require_arg args, 'resource-id'
148
+ name = require_arg args, 'name'
149
+ value = api.resource(id).annotations[name]
150
+ puts value unless value.nil?
151
+ end
153
152
  end
154
- end
155
-
156
- desc "Print annotations as JSON"
157
- arg_name 'resource-id'
158
- command :annotations do |c|
159
- c.action do |go, o, args|
160
- id = full_resource_id require_arg args, 'resource-id'
161
- annots = api.resource(id).annotations.to_h
162
- puts annots.to_json
153
+
154
+ resource.desc "Print annotations as JSON"
155
+ resource.arg_name 'resource-id'
156
+ resource.command :annotations do |c|
157
+ c.action do |go, o, args|
158
+ id = full_resource_id require_arg args, 'resource-id'
159
+ annots = api.resource(id).annotations.to_h
160
+ puts annots.to_json
161
+ end
163
162
  end
164
- end
165
-
166
- desc "List all resources"
167
- command :list do |c|
168
- c.desc "Filter by kind"
169
- c.flag [:k, :kind]
170
-
171
- command_options_for_list c
172
-
173
- c.action do |global_options, options, args|
174
- command_impl_for_list global_options, options, args
163
+
164
+ resource.desc "List all resources"
165
+ resource.command :list do |c|
166
+ c.desc "Filter by kind"
167
+ c.flag [:k, :kind]
168
+
169
+ command_options_for_list c
170
+
171
+ c.action do |global_options, options, args|
172
+ command_impl_for_list global_options, options, args
173
+ end
175
174
  end
176
175
  end
177
176
  end
@@ -18,88 +18,116 @@
18
18
  # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
19
  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
20
  #
21
- require 'conjur/authn'
22
- require 'conjur/command'
23
21
 
24
22
  class Conjur::Command::Roles < Conjur::Command
25
- self.prefix = :role
26
-
27
- desc "Create a new role"
28
- arg_name "role"
29
- command :create do |c|
30
- acting_as_option(c)
31
-
32
- c.action do |global_options,options,args|
33
- id = require_arg(args, 'role')
34
- role = api.role(id)
23
+
24
+ desc "Manage roles"
25
+ command :role do |role|
26
+
27
+ role.desc "Create a new role"
28
+ role.arg_name "role"
29
+ role.command :create do |c|
30
+ acting_as_option(c)
35
31
 
36
- if ownerid = options.delete(:ownerid)
37
- options[:acting_as] = ownerid
32
+ c.desc "Output a JSON response with a single field, roleid"
33
+ c.switch "json"
34
+
35
+ c.action do |global_options,options,args|
36
+ id = require_arg(args, 'role')
37
+ role = api.role(id)
38
+
39
+ if ownerid = options.delete(:ownerid)
40
+ options[:acting_as] = ownerid
41
+ end
42
+
43
+ role.create(options)
44
+ if options[:json]
45
+ display({
46
+ roleid: role.roleid
47
+ })
48
+ else
49
+ puts "Created role #{role.roleid}"
50
+ end
38
51
  end
39
-
40
- role.create(options)
41
- puts "Created role #{role.roleid}"
42
52
  end
43
- end
44
-
45
- desc "Determines whether a role exists"
46
- arg_name "role"
47
- command :exists do |c|
48
- c.action do |global_options,options,args|
49
- id = require_arg(args, 'role')
50
- role = api.role(id)
51
- puts role.exists?
53
+
54
+ role.desc "Determines whether a role exists"
55
+ role.arg_name "role"
56
+ role.command :exists do |c|
57
+ c.desc "Output a JSON response with a single field, exists"
58
+ c.switch "json"
59
+
60
+ c.action do |global_options,options,args|
61
+ id = require_arg(args, 'role')
62
+ role = api.role(id)
63
+ if options[:json]
64
+ display({
65
+ exists: role.exists?
66
+ })
67
+ else
68
+ puts role.exists?
69
+ end
70
+ end
52
71
  end
53
- end
54
72
 
55
- desc "Lists role memberships. The role membership list is recursively expanded."
56
- arg_name "role"
57
- command :memberships do |c|
58
- c.action do |global_options,options,args|
59
- roleid = args.shift
60
- role = roleid.nil? && api.current_role || api.role(roleid)
61
- display role.all.map(&:roleid)
73
+ role.desc "Lists role memberships. The role membership list is recursively expanded."
74
+ role.arg_name "role"
75
+
76
+ role.command :memberships do |c|
77
+ c.desc "Whether to show system (internal) roles"
78
+ c.switch [:s, :system]
79
+
80
+ c.action do |global_options,options,args|
81
+ roleid = args.shift
82
+ role = roleid.nil? && api.current_role || api.role(roleid)
83
+ memberships = role.all.map(&:roleid)
84
+ unless options[:system]
85
+ memberships.reject!{|id| id =~ /^.+?:@/}
86
+ end
87
+ display memberships
88
+ end
62
89
  end
63
- end
64
90
 
65
- desc "Lists all direct members of the role. The membership list is not recursively expanded."
66
- arg_name "role"
67
- command :members do |c|
68
- c.desc "Verbose output"
69
- c.switch [:V,:verbose]
70
-
71
- c.action do |global_options,options,args|
72
- role = args.shift || api.user(api.username).roleid
73
- display_members api.role(role).members, options
91
+ role.desc "Lists all direct members of the role. The membership list is not recursively expanded."
92
+ role.arg_name "role"
93
+ role.command :members do |c|
94
+ c.desc "Verbose output"
95
+ c.switch [:V,:verbose]
96
+
97
+ c.action do |global_options,options,args|
98
+ role = args.shift || api.user(api.username).roleid
99
+ display_members api.role(role).members, options
100
+ end
74
101
  end
75
- end
76
102
 
77
- desc "Grant a role to another role. You must have admin permission on the granting role."
78
- arg_name "role member"
79
- command :grant_to do |c|
80
- c.desc "Whether to grant with admin option"
81
- c.switch [:a,:admin]
82
-
83
- c.action do |global_options,options,args|
84
- id = require_arg(args, 'role')
85
- member = require_arg(args, 'member')
86
- role = api.role(id)
87
- grant_options = {}
88
- grant_options[:admin_option] = true if options[:admin]
89
- role.grant_to member, grant_options
90
- puts "Role granted"
103
+ role.desc "Grant a role to another role. You must have admin permission on the granting role."
104
+ role.arg_name "role member"
105
+ role.command :grant_to do |c|
106
+ c.desc "Whether to grant with admin option"
107
+ c.switch [:a,:admin]
108
+
109
+ c.action do |global_options,options,args|
110
+ id = require_arg(args, 'role')
111
+ member = require_arg(args, 'member')
112
+ role = api.role(id)
113
+ grant_options = {}
114
+ grant_options[:admin_option] = true if options[:admin]
115
+ role.grant_to member, grant_options
116
+ puts "Role granted"
117
+ end
91
118
  end
92
- end
93
119
 
94
- desc "Revoke a role from another role. You must have admin permission on the revoking role."
95
- arg_name "role member"
96
- command :revoke_from do |c|
97
- c.action do |global_options,options,args|
98
- id = require_arg(args, 'role')
99
- member = require_arg(args, 'member')
100
- role = api.role(id)
101
- role.revoke_from member
102
- puts "Role revoked"
120
+ role.desc "Revoke a role from another role. You must have admin permission on the revoking role."
121
+ role.arg_name "role member"
122
+ role.command :revoke_from do |c|
123
+ c.action do |global_options,options,args|
124
+ id = require_arg(args, 'role')
125
+ member = require_arg(args, 'member')
126
+ role = api.role(id)
127
+ role.revoke_from member
128
+ puts "Role revoked"
129
+ end
103
130
  end
104
131
  end
132
+
105
133
  end