enfcli 4.0.0 → 5.0.0.pre.alpha

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.
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2018 Xaptum,Inc
2
+ # Copyright 2018-2020 Xaptum,Inc
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -13,252 +13,300 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- require 'enfthor'
17
- require 'enfapi'
16
+ require "enfthor"
17
+ require "enfapi/user"
18
18
 
19
19
  module EnfCli
20
-
21
20
  module Cmd
22
-
21
+ ##
22
+ # This class handles the commands that maniupulate users and roles
23
23
  class User < EnfThor
24
24
  no_commands {
25
- def display_invites invites
26
- headings = ['Id', 'User Name', 'Full Name', 'Invited By', 'Invite Code']
27
- rows = invites.map{ |hash|
28
- [ hash[:id], hash[:email], hash[:name], hash[:invited_by], hash[:invite_token] ]
25
+ def display_invites(invites)
26
+ headings = ["Id", "User Name", "Full Name", "Invited By", "Invite Code"]
27
+ rows = invites.map { |hash|
28
+ [hash[:id], hash[:email], hash[:name], hash[:created_by], hash[:invite_token]]
29
29
  }
30
30
 
31
31
  render_table(headings, rows)
32
32
  end
33
33
 
34
- def display_users users
35
- headings = ['Id', 'User Name', 'Full Name', 'Last Login', 'Type', 'Reset Code', 'Reset Time', 'Status']
36
- rows = users.map{ |hash|
37
- [ hash[:user_id], hash[:username], hash[:full_name], hash[:last_login], hash[:type], hash[:reset_code],
38
- format_date(hash[:reset_time]), hash[:status] ]
39
- }
34
+ def display_users(users)
35
+ headings = ["Id", "Name", "Username", "Domain", "Last Login", "Status"]
36
+ rows = []
37
+ users.each do |hash|
38
+ hash[:roles].each do |role|
39
+ rows.push [hash[:id],
40
+ hash[:full_name],
41
+ hash[:username],
42
+ hash[:domain],
43
+ hash[:last_login],
44
+ hash[:status]]
45
+ end
46
+ end
40
47
  render_table(headings, rows)
41
48
  end
42
49
 
43
- def send_invite options, user_type
44
- # Get options
45
- domain_network = options.domain
46
-
47
- # get params
48
- name = options[:'name'].join(" ").gsub(/\A"+(.*?)"+\Z/m, '\1')
49
- email = options[:'email']
50
-
51
- # call api
52
- hash = { :email => email, :full_name => name, :welcome_text => "", :user_type => user_type }
53
- data = EnfApi::API.instance.invite domain_network, hash
54
- invite = data[:data]
55
- display_invites invite
56
- end
57
- }
50
+ # Display the roles as a table
51
+ def display_roles(roles)
52
+ headings = ["Cidr", "Role"]
58
53
 
59
- desc "invite-read-only-user", "Invite a domain user"
60
- method_option :domain, :default => nil, :type => :string, :aliases => "-d"
61
- method_option :'name', :type => :array, :required => true, :banner => "NAME"
62
- method_option :'email', :type => :string, :required => true, :banner => "EMAIL"
63
- def invite_read_only_user
64
- try_with_rescue_in_session do
65
- # use the domain network of the user
66
- domain_network = EnfCli::CTX.instance.session[:domain_network]
67
- raise EnfCli::ERROR, "User not in a valid domain!" unless domain_network
68
-
69
- # Get user role
70
- user_role = EnfCli::CTX.instance.session[:type]
71
-
72
- # check user roles
73
- if user_role == "XAPTUM_ADMIN"
74
- raise "--domain is required" unless options[:domain]
75
- else
76
- say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow if options[:domain]
77
- options[:domain] = domain_network
54
+ rows = roles.map do |role|
55
+ [role[:cidr], role[:role]]
78
56
  end
79
57
 
80
- send_invite options, "DOMAIN_USER"
58
+ render_table(headings, rows)
81
59
  end
82
- end
83
60
 
84
- desc "invite-domain-admin-user", "Invite a domain administrator"
85
- method_option :domain, :default => nil, :type => :string, :aliases => "-d"
86
- method_option :'name', :type => :array, :required => true, :banner => "NAME"
87
- method_option :'email', :type => :string, :required => true, :banner => "EMAIL"
88
- def invite_domain_admin_user
89
- try_with_rescue_in_session do
90
- # use the domain network of the user
91
- domain_network = EnfCli::CTX.instance.session[:domain_network]
92
- raise EnfCli::ERROR, "User not in a valid domain!" unless domain_network
93
-
94
- # Get user role
95
- user_role = EnfCli::CTX.instance.session[:type]
96
-
97
- # check user roles
98
- if user_role == "XAPTUM_ADMIN"
99
- raise "--domain is required" unless options[:domain]
100
- else
101
- say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow if options[:domain]
102
- options[:domain] = domain_network
103
- end
104
-
105
- send_invite options, "DOMAIN_ADMIN"
61
+ def display_user_details(user)
62
+ display_users([user])
63
+ display_roles(user[:roles])
106
64
  end
107
- end
65
+ }
108
66
 
109
- desc "invite-enf-admin-user", "Invite an ENF administrator"
110
- method_option :'name', :type => :array, :required => true, :banner => "NAME"
111
- method_option :'email', :type => :string, :required => true, :banner => "EMAIL"
112
- def invite_enf_admin_user
67
+ desc "send-invite",
68
+ "Send an invite to a new user or one with a modified role."
69
+ method_option :email, type: :string, required: true, banner: "EMAIL",
70
+ desc: "Full email address of user to invite."
71
+ method_option :name, type: :array, required: true, banner: "NAME",
72
+ desc: "Full name of user to invite."
73
+ method_option :domain, type: :string, default: nil, banner: "DOMAIN",
74
+ aliases: "-d"
75
+ method_option :network, type: :string, default: nil, banner: "NETWORK",
76
+ aliases: "-n"
77
+ method_option :role, type: :string, default: nil, banner: "ROLE",
78
+ aliases: "-r"
79
+
80
+ def send_invite
113
81
  try_with_rescue_in_session do
114
- # Get user role
115
- user_role = EnfCli::CTX.instance.session[:type]
82
+ # get params
83
+ name = options[:name].join(" ").gsub(/\A"+(.*?)"+\Z/m, '\1')
84
+ email = options[:email]
116
85
 
117
- raise EnfCli::ERROR, "Only ENF Administrators can invite ENF Administrator" unless user_role == "XAPTUM_ADMIN"
86
+ # get correct domain
87
+ domain = EnfCli::CTX.instance.session[:domain]
88
+ raise EnfCli::ERROR, "User not in a valid domain!" unless domain
118
89
 
119
- options[:domain] = EnfCli::CTX.instance.session[:domain_network]
120
- send_invite options, "XAPTUM_ADMIN"
121
- end
122
- end
90
+ # check if admin
91
+ if EnfCli::CTX.instance.xaptum_admin?
92
+ raise EnfCli::ERROR, "--domain is required" unless options[:domain]
123
93
 
124
- desc "invite-iam-admin-user", "Invite an IAM administrator"
125
- method_option :'name', :type => :array, :required => true, :banner => "NAME"
126
- method_option :'email', :type => :string, :required => true, :banner => "EMAIL"
127
- def invite_iam_admin_user
128
- try_with_rescue_in_session do
129
- # Get user role
130
- user_role = EnfCli::CTX.instance.session[:type]
94
+ domain = options[:domain]
95
+ elsif options[:domain]
96
+ say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow
97
+ end
131
98
 
132
- raise EnfCli::ERROR, "Only ENF Administrators can invite IAM Administrator" unless user_role == "XAPTUM_ADMIN"
99
+ invite_hash = { email: email,
100
+ full_name: name,
101
+ domain: domain }
133
102
 
134
- options[:domain] = EnfCli::CTX.instance.session[:domain_network]
135
- send_invite options, "IAM_ADMIN"
136
- end
137
- end
103
+ role = options[:role]
104
+ role = role.upcase if role
105
+ network = options[:network]
138
106
 
139
- desc "invite-captive-admin-user", "Invite a captive administrator"
140
- method_option :'captive-domain', :type => :string, :required => true, :banner => "CAPTIVE CONTROL DOMAIN"
141
- method_option :'name', :type => :array, :required => true, :banner => "NAME"
142
- method_option :'email', :type => :string, :required => true, :banner => "EMAIL"
143
- def invite_captive_admin_user
144
- try_with_rescue_in_session do
145
- # Get user role
146
- user_role = EnfCli::CTX.instance.session[:type]
107
+ roles_hash = nil
147
108
 
148
- raise EnfCli::ERROR, "Only ENF Administrators can invite CAPTIVE Administrator" unless user_role == "XAPTUM_ADMIN"
109
+ case role
110
+ when "XAPTUM_ADMIN", "IAM_ADMIN"
111
+ roles_hash = [{ cidr: "::/0", role: role }]
112
+ when "DOMAIN_ADMIN", "DOMAIN_USER", "CAPTIVE_ADMIN"
113
+ roles_hash = [{ cidr: domain, role: role }]
114
+ when "NETWORK_ADMIN", "NETWORK_USER"
115
+ roles_hash = [{ cidr: network, role: role }]
116
+ end
149
117
 
150
- options[:domain] = options[:'captive-domain']
151
- send_invite options, "CAPTIVE_ADMIN"
118
+ if roles_hash
119
+ invite_hash[:roles] = roles_hash
120
+ end
121
+
122
+ resp_data = EnfApi::UserManager.instance.invite invite_hash
123
+ invite = resp_data[:data]
124
+ display_invites invite
152
125
  end
153
126
  end
154
127
 
128
+ desc "delete-invite", "Delete an invite"
129
+ method_option :id, type: :string, required: true
155
130
 
156
- desc "cancel-user-invite", "Cancel an invite"
157
- method_option :email, :type => :string, :required => true
158
- def cancel_user_invite
131
+ def delete_invite
159
132
  try_with_rescue_in_session do
133
+ id = options[:id]
160
134
  # call api
161
- EnfApi::API.instance.cancel_invite options.email
162
-
163
- # print success
164
- say "Invite Canceled!", :green
135
+ EnfApi::UserManager.instance.delete_invite id
136
+ say "Invite: #{id} successfully deleted", :green
165
137
  end
166
138
  end
167
139
 
168
- desc "resend-user-invite", "Resend an invite"
169
- method_option :email, :type => :string, :required => true
170
- def resend_user_invite
140
+ desc "resend-invite", "Resend an invite"
141
+ method_option :id, type: :string, required: true
142
+
143
+ def resend_invite
171
144
  try_with_rescue_in_session do
145
+ id = options[:id]
172
146
  # call api
173
- EnfApi::API.instance.resend_invite options.email
174
-
175
- # print success
176
- say "Resent invite email!", :green
147
+ EnfApi::UserManager.instance.resend_invite id
148
+ say "Resent invite: #{id}!", :green
177
149
  end
178
150
  end
179
151
 
180
152
  desc "list-invites", "List user invites"
181
153
  method_option :domain, :default => nil, :type => :string, :aliases => "-d"
154
+
182
155
  def list_invites
183
156
  try_with_rescue_in_session do
184
157
  # use the domain network of the user
185
- domain_network = EnfCli::CTX.instance.session[:domain_network]
186
- raise EnfCli::ERROR, "User not in a valid domain!" unless domain_network
158
+ domain = nil
187
159
 
188
- # Get user role
189
- user_role = EnfCli::CTX.instance.session[:type]
190
-
191
- # check user roles
192
- if user_role == "XAPTUM_ADMIN"
193
- domain_network = options[:domain] if options[:domain]
194
- else
195
- say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow if options[:domain]
160
+ # only XAPTUM_ADMIN can specify --domain (but doesn't have to)
161
+ if EnfCli::CTX.instance.xaptum_admin?
162
+ domain = options[:domain] if options[:domain]
163
+ elsif options[:domain]
164
+ say "Warning: Ignoring command option --domain #{options[:domain]}", :yellow
196
165
  end
197
166
 
198
167
  # call the api
199
- data = EnfApi::API.instance.list_domain_invites domain_network
168
+ data = EnfApi::UserManager.instance.list_invites domain
200
169
  invites = data[:data]
201
170
 
202
171
  display_invites invites
203
172
  end
204
173
  end
205
174
 
206
- desc "list-users", "List users"
207
- method_option :domain, :default => nil, :type => :string, :aliases => "-d"
208
- def list_users
175
+ desc "get-user-details", "Get User Details"
176
+ method_option :email, required: true, type: :string, banner: "EMAIL",
177
+ aliases: "-e"
178
+
179
+ def get_user_details
209
180
  try_with_rescue_in_session do
210
- # use the domain network of the user
211
- domain_network = EnfCli::CTX.instance.session[:domain_network]
212
- raise EnfCli::ERROR, "User not in a valid domain!" unless domain_network
181
+ # call the api
182
+ data = EnfApi::UserManager.instance.get_user options[:email]
183
+ user = data[:data][0]
184
+
185
+ display_user_details user
186
+ end
187
+ end
213
188
 
214
- # Get user role
215
- user_role = EnfCli::CTX.instance.session[:type]
189
+ desc "list-users", "List users"
190
+ method_option :domain, default: nil, type: :string, banner: "DOMAIN",
191
+ aliases: "-d"
192
+ method_option :network, default: nil, type: :string, banner: "NETWORK",
193
+ aliases: "-n"
216
194
 
217
- # check user roles
218
- if user_role == "XAPTUM_ADMIN"
219
- domain_network = options[:domain] if options[:domain]
220
- else
221
- say "Warning: Ignoring command option -d #{options[:domain]}", :yellow if options[:domain]
195
+ def list_users
196
+ try_with_rescue_in_session do
197
+ domain = options[:domain]
198
+ network = options[:network]
199
+
200
+ ## initalize query param
201
+ query_param = ""
202
+ if domain
203
+ query_param = "?domain=#{domain}"
204
+ elsif network
205
+ query_param = "?network=#{network}"
222
206
  end
223
207
 
224
208
  # call the api
225
- data = EnfApi::API.instance.list_domain_users domain_network
209
+ data = EnfApi::UserManager.instance.list_users query_param
226
210
  users = data[:data]
227
211
 
228
212
  display_users users
229
213
  end
230
214
  end
231
215
 
216
+ desc "list-user-roles", "List user roles"
217
+ method_option :email, type: :string, required: true, banner: "EMAIL"
218
+ method_option :network, default: nil, type: :string, banner: "NETWORK",
219
+ aliases: "-n"
220
+
221
+ def list_user_roles
222
+ try_with_rescue_in_session do
223
+ # call api
224
+ data = EnfApi::UserManager.instance.list_user_roles options[:email], options[:network]
225
+ roles = data[:data]
226
+
227
+ # print roles
228
+ display_roles roles
229
+ end
230
+ end
231
+
232
+ desc "delete-user-roles", "Remove a user's roles"
233
+ method_option :email, type: :string, required: true, banner: "EMAIL"
234
+ method_option :network, default: nil, type: :string, banner: "NETWORK",
235
+ aliases: "-n",
236
+ desc: 'Can be a /64 cidr or "ALL"'
237
+ method_option :roles, type: :string, required: true, banner: "ROLES",
238
+ aliases: "-r",
239
+ desc: "Can be a valid DOMAIN or NETWORK role. " \
240
+ "Can take '*' wildcards."
241
+
242
+ def delete_user_roles
243
+ try_with_rescue_in_session do
244
+ user_id = options[:email]
245
+ roles = options[:roles]
246
+ roles = roles.upcase if roles
247
+ network = options[:network]
248
+
249
+ if roles[0..6] == "NETWORK" && !network
250
+ raise EnfCli::ERROR, "--network option must be included for --roles=#{roles}"
251
+ end
252
+
253
+ EnfApi::UserManager.instance.delete_user_roles user_id, roles, network
254
+ say "Role: #{roles} successfully removed from user: #{user_id}", :green
255
+ end
256
+ end
257
+
232
258
  desc "deactivate-user", "Deactivate User"
233
- method_option :user_id, :required => true, :type => :numeric
259
+ method_option :email, required: true, type: :string, banner: "EMAIL"
260
+
234
261
  def deactivate_user
235
262
  try_with_rescue_in_session do
236
-
237
263
  ## call the api
238
- status = { :status => "INACTIVE" }
239
- EnfApi::API.instance.update_user_status options[:user_id], status
264
+ status = { status: "INACTIVE" }
265
+ EnfApi::UserManager.instance.update_user_status options[:email], status
240
266
 
241
267
  say "Deactivated user!", :green
268
+ end
269
+ end
242
270
 
271
+ desc "add-user-role", "Add a new role to the specified rule."
272
+ method_option :email, type: :string, required: true, banner: "EMAIL"
273
+ method_option :cidr, type: :string, required: true, banner: "CIDR",
274
+ desc: "Can be a /64 cidr for NETWORK user or " \
275
+ "/48 cidr for DOMAIN user."
276
+ method_option :role, type: :string, required: true, banner: "ROLE",
277
+ aliases: "-r",
278
+ desc: "Can be a valid DOMAIN or NETWORK role. ",
279
+ enum: ["XAPTUM_ADMIN", "DOMAIN_ADMIN", "DOMAIN_USER", "NETWORK_ADMIN", "NETWORK_USER", "CAPTIVE_ADMIN", "IAM_ADMIN"]
280
+
281
+ def add_user_role
282
+ try_with_rescue_in_session do
283
+ ## get options
284
+ email = options[:email]
285
+ role = options[:role]
286
+ role = role.upcase if role
287
+ cidr = EnfCli::IPV6Cidr.new(options[:cidr]).to_s
288
+
289
+ ## call api
290
+ role_hash = [{ cidr: cidr, role: role }]
291
+ resp = EnfApi::UserManager.instance.add_user_role email, role_hash
292
+ resp_roles = resp[:data]
293
+
294
+ ## display response
295
+ display_roles resp_roles
243
296
  end
244
297
  end
245
298
 
246
299
  desc "activate-user", "Activate User"
247
- method_option :user_id, :required => true, :type => :numeric
300
+ method_option :email, required: true, type: :string, banner: "EMAIL"
301
+
248
302
  def activate_user
249
303
  try_with_rescue_in_session do
250
-
251
304
  ## call the api
252
- status = { :status => "ACTIVE" }
253
- EnfApi::API.instance.update_user_status options[:user_id], status
254
-
305
+ status = { status: "ACTIVE" }
306
+ EnfApi::UserManager.instance.update_user_status options[:email], status
255
307
  say "Activated user!", :green
256
-
257
308
  end
258
309
  end
259
-
260
310
  end
261
-
262
311
  end
263
-
264
312
  end