enfcli 4.0.0 → 5.0.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/Dockerfile +2 -2
- data/.circleci/config.yml +5 -0
- data/Gemfile.lock +38 -26
- data/Makefile +7 -0
- data/README.md +52 -7
- data/enfcli.gemspec +28 -26
- data/format.sh +9 -0
- data/lib/enfapi.rb +184 -237
- data/lib/enfapi/dns.rb +95 -0
- data/lib/enfapi/firewall.rb +37 -0
- data/lib/enfapi/user.rb +75 -0
- data/lib/enfcli.rb +211 -111
- data/lib/enfcli/commands/captive.rb +518 -157
- data/lib/enfcli/commands/user.rb +208 -160
- data/lib/enfcli/commands/xcr.rb +151 -119
- data/lib/enfcli/commands/xdns.rb +65 -55
- data/lib/enfcli/commands/xfw.rb +37 -37
- data/lib/enfcli/commands/xiam.rb +87 -80
- data/lib/enfcli/version.rb +2 -2
- data/lib/enfthor.rb +38 -14
- metadata +65 -5
data/lib/enfapi/dns.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2020 Xaptum,Inc
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
module EnfApi
|
17
|
+
class Dns
|
18
|
+
include Singleton
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@version = "v1"
|
22
|
+
@xdns_base_url = "/api/xdns/#{@version}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def list_zones(domain)
|
26
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/zones?enf_domain=#{domain}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_dns_zone(new_zone)
|
30
|
+
json = EnfApi::to_json(new_zone)
|
31
|
+
EnfApi::API.instance.post "#{@xdns_base_url}/zones", json
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_dns_zone(zone_id, updated_zone)
|
35
|
+
json = EnfApi::to_json(updated_zone)
|
36
|
+
EnfApi::API.instance.put "#{@xdns_base_url}/zones/#{zone_id}", json
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_dns_zone(zone_id)
|
40
|
+
EnfApi::API.instance.delete "#{@xdns_base_url}/zones/#{zone_id}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_networks_to_zone(zone_id, add_networks)
|
44
|
+
json = EnfApi::to_json(add_networks)
|
45
|
+
EnfApi::API.instance.post "#{@xdns_base_url}/zones/#{zone_id}/networks", json
|
46
|
+
end
|
47
|
+
|
48
|
+
def replace_networks_in_zone(zone_id, replace_networks)
|
49
|
+
json = EnfApi::to_json(replace_networks)
|
50
|
+
EnfApi::API.instance.put "#{@xdns_base_url}/zones/#{zone_id}/networks", json
|
51
|
+
end
|
52
|
+
|
53
|
+
def list_networks_in_zone(zone_id)
|
54
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/zones/#{zone_id}/networks"
|
55
|
+
end
|
56
|
+
|
57
|
+
def delete_networks_from_zone(zone_id, delete_networks)
|
58
|
+
EnfApi::API.instance.delete "#{@xdns_base_url}/zones/#{zone_id}/networks?delete=#{delete_networks}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def list_zones_in_network(network)
|
62
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/networks/#{network}/zones"
|
63
|
+
end
|
64
|
+
|
65
|
+
def provision_server(network, new_server)
|
66
|
+
json = EnfApi::to_json(new_server)
|
67
|
+
EnfApi::API.instance.post "#{@xdns_base_url}/networks/#{network}/servers", json
|
68
|
+
end
|
69
|
+
|
70
|
+
def list_servers(network)
|
71
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/networks/#{network}/servers"
|
72
|
+
end
|
73
|
+
|
74
|
+
def delete_server(network, server_ipv6)
|
75
|
+
EnfApi::API.instance.delete "#{@xdns_base_url}/networks/#{network}/servers/#{server_ipv6}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_dns_record(zone_id, new_record)
|
79
|
+
json = EnfApi::to_json(new_record)
|
80
|
+
EnfApi::API.instance.post "#{@xdns_base_url}/zones/#{zone_id}/records", json
|
81
|
+
end
|
82
|
+
|
83
|
+
def list_dns_records(zone_id)
|
84
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/zones/#{zone_id}/records"
|
85
|
+
end
|
86
|
+
|
87
|
+
def query(network, type, name)
|
88
|
+
EnfApi::API.instance.get "#{@xdns_base_url}/networks/#{network}/query/#{type}/#{name}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def delete_dns_record(record_id)
|
92
|
+
EnfApi::API.instance.delete "#{@xdns_base_url}/records/#{record_id}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2020 Xaptum,Inc
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
require "singleton"
|
18
|
+
|
19
|
+
module EnfApi
|
20
|
+
class Firewall
|
21
|
+
include Singleton
|
22
|
+
|
23
|
+
def list_firewall_rules(network)
|
24
|
+
EnfApi::API.instance.get "/api/xfw/v1/#{network}/rule"
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_firewall_rule(network, rule)
|
28
|
+
rule_json = EnfApi::to_json(rule)
|
29
|
+
EnfApi::API.instance.post "/api/xfw/v1/#{network}/rule", rule_json
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete_firewall_rules(network, id = nil)
|
33
|
+
# Same method to call to delete all firewall rules in a network. if id is nil
|
34
|
+
EnfApi::API.instance.delete "/api/xfw/v1/#{network}/rule/#{id}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/enfapi/user.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2020 Xaptum,Inc
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
module EnfApi
|
17
|
+
class UserManager
|
18
|
+
include Singleton
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@version = "v3"
|
22
|
+
@xcr_base_url = "/api/xcr/#{@version}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def list_users(query)
|
26
|
+
EnfApi::API.instance.get "#{@xcr_base_url}/users#{query}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_user(email)
|
30
|
+
EnfApi::API.instance.get "#{@xcr_base_url}/users/#{email}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def list_user_roles(user, network)
|
34
|
+
url = "#{@xcr_base_url}/users/#{user}/roles"
|
35
|
+
url += "?network=#{network}" if network
|
36
|
+
EnfApi::API.instance.get url
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_user_roles(user_id, roles, network)
|
40
|
+
url = "#{@xcr_base_url}/users/#{user_id}/roles?roles=#{roles}"
|
41
|
+
url += "&network=#{network}" if network
|
42
|
+
EnfApi::API.instance.delete url
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_user_role(user_id, role_hash)
|
46
|
+
json = EnfApi::to_json(role_hash)
|
47
|
+
url = "#{@xcr_base_url}/users/#{user_id}/roles"
|
48
|
+
EnfApi::API.instance.post url, json
|
49
|
+
end
|
50
|
+
|
51
|
+
def list_invites(domain)
|
52
|
+
url = "#{@xcr_base_url}/invites"
|
53
|
+
url += "?domain=#{domain}" if domain
|
54
|
+
EnfApi::API.instance.get url
|
55
|
+
end
|
56
|
+
|
57
|
+
def invite(hash)
|
58
|
+
json = EnfApi::to_json(hash)
|
59
|
+
EnfApi::API.instance.post "#{@xcr_base_url}/invites", json
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete_invite(invite_id)
|
63
|
+
EnfApi::API.instance.delete "#{@xcr_base_url}/invites/#{invite_id}"
|
64
|
+
end
|
65
|
+
|
66
|
+
def resend_invite(invite_id)
|
67
|
+
EnfApi::API.instance.put "#{@xcr_base_url}/invites/#{invite_id}", "{}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def update_user_status(user_id, status)
|
71
|
+
json = EnfApi::to_json(status)
|
72
|
+
EnfApi::API.instance.put "#{@xcr_base_url}/users/#{user_id}/status", json
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/lib/enfcli.rb
CHANGED
@@ -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,26 +13,30 @@
|
|
13
13
|
# See the License for the specific language governing permissions and
|
14
14
|
# limitations under the License.
|
15
15
|
#
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
|
23
|
-
require
|
24
|
-
require
|
25
|
-
|
26
|
-
|
27
|
-
require
|
28
|
-
|
29
|
-
require
|
30
|
-
require
|
31
|
-
require
|
32
|
-
require
|
33
|
-
require
|
16
|
+
require "openssl"
|
17
|
+
require "enfapi"
|
18
|
+
require "enfthor"
|
19
|
+
require "readline"
|
20
|
+
require "singleton"
|
21
|
+
require "ipaddr"
|
22
|
+
require "clipboard"
|
23
|
+
require "json"
|
24
|
+
require "securerandom"
|
25
|
+
|
26
|
+
require "rubygems/commands/update_command"
|
27
|
+
require "rubygems/commands/search_command"
|
28
|
+
|
29
|
+
require "enfcli/version"
|
30
|
+
require "enfcli/commands/xcr"
|
31
|
+
require "enfcli/commands/xiam"
|
32
|
+
require "enfcli/commands/xfw"
|
33
|
+
require "enfcli/commands/user"
|
34
|
+
require "enfcli/commands/captive"
|
35
|
+
require "enfcli/commands/xdns"
|
34
36
|
|
35
37
|
module EnfCli
|
38
|
+
CONFIG_FILE = "#{Dir.home() + "/.xaptum_config.json"}"
|
39
|
+
|
36
40
|
FIREWALL_CMD = "firewall"
|
37
41
|
IAM_CMD = "iam"
|
38
42
|
NETWORK_CMD = "network"
|
@@ -63,13 +67,13 @@ module EnfCli
|
|
63
67
|
@ip.hton
|
64
68
|
end
|
65
69
|
|
66
|
-
def self.ntoh
|
67
|
-
ip = IPAddr::new_ntoh(
|
70
|
+
def self.ntoh(ipv6_bytes)
|
71
|
+
ip = IPAddr::new_ntoh(ipv6_bytes)
|
68
72
|
EnfCli::IPV6.new ip.to_s
|
69
73
|
end
|
70
74
|
|
71
75
|
private
|
72
|
-
|
76
|
+
|
73
77
|
attr_accessor :ip
|
74
78
|
end
|
75
79
|
|
@@ -84,10 +88,9 @@ module EnfCli
|
|
84
88
|
# store in prefix/len
|
85
89
|
@prefix = EnfCli::IPV6.new tokens[0]
|
86
90
|
@len = tokens[1].to_i
|
87
|
-
|
91
|
+
|
88
92
|
# raise if len is not 0
|
89
93
|
raise EnfCli::ERROR, "#{ipv6cidr} is not a valid CIDR notation." unless len > 2
|
90
|
-
|
91
94
|
end
|
92
95
|
|
93
96
|
def prefix_bytes
|
@@ -101,47 +104,47 @@ module EnfCli
|
|
101
104
|
def to_s
|
102
105
|
"#{@prefix.to_s}/#{@len}"
|
103
106
|
end
|
104
|
-
|
107
|
+
|
105
108
|
private
|
109
|
+
|
106
110
|
attr_accessor :prefix, :len
|
107
111
|
end
|
108
|
-
|
112
|
+
|
109
113
|
def self.ask_password(prompt = nil)
|
110
114
|
begin
|
111
|
-
prompt
|
115
|
+
prompt ||= "Enter Password:"
|
112
116
|
print prompt
|
113
117
|
# We hide the entered characters before to ask for the password
|
114
|
-
system
|
118
|
+
system "stty -echo"
|
115
119
|
password = $stdin.gets.chomp
|
116
|
-
system
|
120
|
+
system "stty echo"
|
117
121
|
puts ""
|
118
122
|
return password
|
119
|
-
|
120
123
|
rescue NoMethodError, Interrupt
|
121
124
|
# When the process is exited, we display the characters again
|
122
125
|
# And we exit
|
123
|
-
system
|
126
|
+
system "stty echo"
|
124
127
|
exit
|
125
128
|
end
|
126
129
|
end
|
127
130
|
|
128
131
|
def self.generate_ec_cert(key, ipv6)
|
129
|
-
# monkey patch
|
132
|
+
# monkey patch
|
130
133
|
OpenSSL::PKey::EC.send(:alias_method, :private?, :private_key?)
|
131
|
-
|
134
|
+
|
132
135
|
# Generate cert
|
133
136
|
cert = OpenSSL::X509::Certificate.new
|
134
|
-
cert.subject = cert.issuer = OpenSSL::X509::Name.new([[
|
137
|
+
cert.subject = cert.issuer = OpenSSL::X509::Name.new([["CN", ipv6.to_s]])
|
135
138
|
cert.not_before = Time.now
|
136
139
|
cert.not_after = Time.now + 365 * 24 * 60 * 60
|
137
140
|
cert.public_key = key
|
138
|
-
cert.serial =
|
141
|
+
cert.serial = SecureRandom.random_number(2 ** 159 - 2) + 1
|
139
142
|
cert.version = 2
|
140
|
-
|
141
|
-
cert.sign key, OpenSSL::Digest::
|
143
|
+
|
144
|
+
cert.sign key, OpenSSL::Digest::SHA256.new
|
142
145
|
cert
|
143
|
-
end
|
144
|
-
|
146
|
+
end
|
147
|
+
|
145
148
|
def self.expand_path(file)
|
146
149
|
new_file = File.expand_path(EnfCli::expand_env(file))
|
147
150
|
end
|
@@ -154,7 +157,7 @@ module EnfCli
|
|
154
157
|
ip = EnfCli::IPV6.new ipv6
|
155
158
|
ip.to_s
|
156
159
|
end
|
157
|
-
|
160
|
+
|
158
161
|
##
|
159
162
|
# EnfCli error
|
160
163
|
#
|
@@ -174,38 +177,67 @@ module EnfCli
|
|
174
177
|
|
175
178
|
def initialize
|
176
179
|
@prompt = "enfcli"
|
177
|
-
@host =
|
180
|
+
@host = ""
|
181
|
+
@user = ""
|
182
|
+
@session = nil
|
183
|
+
end
|
184
|
+
|
185
|
+
def init(host, user, session)
|
186
|
+
@host = host
|
187
|
+
@user = user
|
188
|
+
@session = session
|
189
|
+
@prompt = "enfcli-#{user}" if user
|
178
190
|
end
|
179
191
|
|
180
192
|
def xaptum_admin?
|
181
|
-
|
193
|
+
has_role? "XAPTUM_ADMIN"
|
182
194
|
end
|
183
195
|
|
184
|
-
def
|
185
|
-
|
196
|
+
def domain_admin?
|
197
|
+
has_role? "DOMAIN_ADMIN"
|
186
198
|
end
|
187
199
|
|
188
|
-
def
|
189
|
-
"
|
200
|
+
def domain_user?
|
201
|
+
has_role? "DOMAIN_USER"
|
190
202
|
end
|
191
203
|
|
192
|
-
def
|
193
|
-
|
204
|
+
def network_admin?
|
205
|
+
has_role? "NETWORK_ADMIN"
|
194
206
|
end
|
195
207
|
|
196
|
-
def
|
197
|
-
|
208
|
+
def network_user?
|
209
|
+
has_role? "NETWORK_USER"
|
198
210
|
end
|
199
211
|
|
200
|
-
def
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
212
|
+
def edit_domain_role?
|
213
|
+
xaptum_admin? || domain_admin?
|
214
|
+
end
|
215
|
+
|
216
|
+
def has_role?(role)
|
217
|
+
all_roles = @session[:roles]
|
218
|
+
all_roles.each do |cur_role|
|
219
|
+
if cur_role[:role] == role
|
220
|
+
return true
|
221
|
+
end
|
205
222
|
end
|
206
|
-
|
223
|
+
false
|
224
|
+
end
|
225
|
+
|
226
|
+
def host
|
227
|
+
@host.to_s
|
228
|
+
end
|
229
|
+
|
230
|
+
def auth_token
|
231
|
+
@session[:token]
|
232
|
+
end
|
233
|
+
|
234
|
+
def user
|
235
|
+
@user
|
236
|
+
end
|
237
|
+
|
238
|
+
def prompt
|
239
|
+
"\001\033[1;32m\002#{@prompt}>\001\033[0m\002 "
|
207
240
|
end
|
208
|
-
|
209
241
|
end
|
210
242
|
|
211
243
|
##
|
@@ -213,75 +245,112 @@ module EnfCli
|
|
213
245
|
#
|
214
246
|
class CLI < EnfThor
|
215
247
|
no_commands {
|
216
|
-
def execute_gem_cmd
|
217
|
-
begin
|
248
|
+
def execute_gem_cmd(cmd)
|
249
|
+
begin
|
218
250
|
cmd.execute
|
219
251
|
rescue Gem::SystemExitException => e
|
220
252
|
say "Unable to execute commnad. Please try again!", :red
|
221
253
|
end
|
222
254
|
end
|
223
255
|
}
|
224
|
-
|
256
|
+
|
225
257
|
desc "connect", "Connect to ENF Controller"
|
226
|
-
method_option :host, :
|
227
|
-
method_option :user, :
|
228
|
-
|
258
|
+
method_option :host, type: :string
|
259
|
+
method_option :user, type: :string
|
260
|
+
|
229
261
|
def connect(*names)
|
230
|
-
host =
|
231
|
-
user =
|
262
|
+
host = ""
|
263
|
+
user = ""
|
232
264
|
|
233
265
|
try_with_rescue do
|
266
|
+
## first check for command options
|
267
|
+
if options[:host] && options[:user]
|
268
|
+
host = options[:host]
|
269
|
+
user = options[:user]
|
270
|
+
elsif File.file?(CONFIG_FILE) # then check for config file
|
271
|
+
config_json = JSON.load(File.open(CONFIG_FILE))
|
272
|
+
host = config_json["host"]
|
273
|
+
user = config_json["user"]
|
274
|
+
else
|
275
|
+
raise EnfCli::ERROR, "You must either specify the --host and --user parameters or create a Xaptum config file to connect."
|
276
|
+
end
|
277
|
+
|
234
278
|
# Make sure to use https as default
|
235
279
|
host = "https://#{host}" unless host =~ /^(http|https):\/\//
|
236
|
-
|
280
|
+
|
237
281
|
# Ask for password
|
238
282
|
say "Connecting to '#{host}'.....", :bold
|
239
283
|
password = EnfCli::ask_password()
|
240
|
-
|
284
|
+
|
241
285
|
# Authenticate
|
242
286
|
resp = EnfApi::API.instance.authenticate(host, user, password)
|
243
287
|
|
244
|
-
#
|
245
|
-
EnfCli::CTX.instance.
|
288
|
+
# initialize CTX
|
289
|
+
EnfCli::CTX.instance.init host, user, resp[:data][0]
|
246
290
|
|
247
291
|
# launch shell/exec cmd
|
248
292
|
if names.empty?
|
249
|
-
EnfCli::Shell::Console::start
|
293
|
+
EnfCli::Shell::Console::start
|
250
294
|
else
|
251
295
|
EnfCli::Shell::CLI.start names
|
252
296
|
end
|
253
297
|
end
|
254
|
-
end
|
298
|
+
end
|
255
299
|
|
256
300
|
map %w[--version -v] => :__print_version
|
257
301
|
desc "--version, -v", "print the version"
|
302
|
+
|
258
303
|
def __print_version
|
259
304
|
puts EnfCli::VERSION
|
260
305
|
end
|
261
306
|
|
262
|
-
desc "update", "", :
|
307
|
+
desc "update", "", hide: true
|
308
|
+
|
263
309
|
def update
|
264
310
|
cmd = Gem::Commands::UpdateCommand.new
|
265
|
-
cmd.handle_options [
|
311
|
+
cmd.handle_options ["enfcli"]
|
266
312
|
execute_gem_cmd cmd
|
267
313
|
end
|
268
314
|
|
269
|
-
desc "search", "", :
|
315
|
+
desc "search", "", hide: true
|
316
|
+
|
270
317
|
def search
|
271
318
|
cmd = Gem::Commands::SearchCommand.new
|
272
319
|
cmd.handle_options ["enfcli"]
|
273
320
|
execute_gem_cmd cmd
|
274
321
|
end
|
275
322
|
|
323
|
+
desc "create-config-file", "Create a Xaptum configuration file in your home directory"
|
324
|
+
method_option :host, type: :string, required: true
|
325
|
+
method_option :user, type: :string, required: true
|
326
|
+
|
327
|
+
def create_config_file
|
328
|
+
host = options[:host]
|
329
|
+
user = options[:user]
|
330
|
+
config_file = File.new(CONFIG_FILE, "w+")
|
331
|
+
config_file.puts({ host: host, user: user }.to_json)
|
332
|
+
config_file.close
|
333
|
+
say "Config file created successfully at #{CONFIG_FILE}!", :green
|
334
|
+
end
|
335
|
+
|
336
|
+
desc "display-config-file", "Displays your Xaptum configuraton file, if it exists"
|
337
|
+
|
338
|
+
def display_config_file
|
339
|
+
file = CONFIG_FILE
|
340
|
+
|
341
|
+
## return if file not found
|
342
|
+
raise EnfCli::ERROR, "#{file} not found!" unless File.exists?(file)
|
343
|
+
|
344
|
+
say File.readlines(file).join
|
345
|
+
end
|
346
|
+
|
276
347
|
default_task :connect
|
277
|
-
|
278
348
|
end
|
279
349
|
|
280
350
|
##
|
281
351
|
# Shell Module
|
282
352
|
#
|
283
353
|
module Shell
|
284
|
-
|
285
354
|
class Console
|
286
355
|
class << self
|
287
356
|
def execute(input)
|
@@ -289,61 +358,57 @@ module EnfCli
|
|
289
358
|
argv = input.split(/[\s=](?=(?:[^"]|"[^"]*")*$)/)
|
290
359
|
# now remove quotes.
|
291
360
|
argv.each do |arg|
|
292
|
-
arg.gsub!(/\A"|"\Z/,
|
361
|
+
arg.gsub!(/\A"|"\Z/, "")
|
293
362
|
end
|
294
363
|
EnfCli::Shell::CLI.start(argv)
|
295
364
|
end
|
296
365
|
|
297
|
-
def start
|
366
|
+
def start
|
298
367
|
$stdout.sync = true
|
299
|
-
# Set prompt
|
300
|
-
EnfCli::CTX.instance.prompt = user
|
301
368
|
|
302
|
-
# Set host
|
303
|
-
EnfCli::CTX.instance.host = host
|
304
|
-
|
305
369
|
# Read each line
|
306
|
-
comp = proc { |s| Readline::HISTORY.grep(/^#{Regexp.escape(s)}/) }
|
370
|
+
comp = proc { |s| Readline::HISTORY.grep(/^#{Regexp.escape(s)}/) }
|
307
371
|
Readline.completion_append_character = " "
|
308
372
|
Readline.completion_proc = comp
|
309
373
|
|
310
374
|
stty_save = `stty -g`.chomp
|
311
|
-
trap(
|
312
|
-
|
375
|
+
trap("INT") { system("stty", stty_save); exit }
|
376
|
+
|
313
377
|
while input = Readline.readline(EnfCli::CTX.instance.prompt, true)
|
314
|
-
break if input == "exit" or input ==
|
315
|
-
|
378
|
+
break if input == "exit" or input == '\q' or input == "quit"
|
379
|
+
|
316
380
|
# Remove blank lines from history
|
317
381
|
Readline::HISTORY.pop if input == ""
|
318
|
-
|
382
|
+
|
319
383
|
execute(input) unless input == ""
|
320
384
|
end
|
321
|
-
end
|
322
|
-
end
|
385
|
+
end
|
386
|
+
end
|
323
387
|
end
|
324
388
|
|
325
389
|
# Shell CLI class
|
326
|
-
class CLI < EnfThor
|
390
|
+
class CLI < EnfCli::EnfThor
|
391
|
+
desc "ls [<dir>]", "List files in a directory", hide: true
|
392
|
+
method_option :dir, type: :string, required: false
|
327
393
|
|
328
|
-
desc "ls [<dir>]", "List files in a directory", :hide => true
|
329
|
-
method_option :dir, :type => :string, :required => false
|
330
394
|
def ls(dir = nil)
|
331
395
|
try_with_rescue do
|
332
|
-
dir
|
333
|
-
dir = EnfCli::expand_path(
|
334
|
-
|
335
|
-
Dir.entries(
|
336
|
-
puts f unless f.start_with?(
|
396
|
+
dir ||= "."
|
397
|
+
dir = EnfCli::expand_path(dir)
|
398
|
+
|
399
|
+
Dir.entries(dir).each { |f|
|
400
|
+
puts f unless f.start_with?(".")
|
337
401
|
}
|
338
402
|
end
|
339
403
|
end
|
340
404
|
|
341
|
-
desc "cat <file>", "Display contents of a file", :
|
405
|
+
desc "cat <file>", "Display contents of a file", hide: true
|
406
|
+
|
342
407
|
def cat(file)
|
343
408
|
try_with_rescue do
|
344
409
|
# expand path
|
345
410
|
file = EnfCli::expand_path(file)
|
346
|
-
|
411
|
+
|
347
412
|
## return if keyfile not found
|
348
413
|
raise EnfCli::ERROR, "#{file} not found!" unless File.exists?(file)
|
349
414
|
|
@@ -351,43 +416,79 @@ module EnfCli
|
|
351
416
|
end
|
352
417
|
end
|
353
418
|
|
354
|
-
desc "pwd", "Current Working Directory", :
|
419
|
+
desc "pwd", "Current Working Directory", hide: true
|
420
|
+
|
355
421
|
def pwd
|
356
422
|
try_with_rescue do
|
357
423
|
say Dir.pwd
|
358
424
|
end
|
359
425
|
end
|
360
426
|
|
361
|
-
desc "cd [<dir>]", "Change working directory", :
|
427
|
+
desc "cd [<dir>]", "Change working directory", hide: true
|
428
|
+
|
362
429
|
def cd(dir = "~")
|
363
430
|
try_with_rescue do
|
364
|
-
dir = EnfCli::expand_path(
|
431
|
+
dir = EnfCli::expand_path(dir)
|
365
432
|
raise EnfCli::ERROR, "No such directory #{dir}" unless Dir.exist?(dir)
|
433
|
+
|
366
434
|
Dir.chdir(dir)
|
367
435
|
end
|
368
436
|
end
|
369
437
|
|
370
|
-
desc "host", "Display ENF Controller host", :
|
438
|
+
desc "host", "Display ENF Controller host", hide: true
|
439
|
+
|
371
440
|
def host
|
372
441
|
try_with_rescue do
|
373
442
|
say EnfCli::CTX.instance.host, :bold
|
374
443
|
end
|
375
444
|
end
|
376
|
-
|
377
|
-
desc "clear", "Clear Terminal Screen", :
|
445
|
+
|
446
|
+
desc "clear", "Clear Terminal Screen", hide: true
|
447
|
+
|
378
448
|
def clear
|
379
449
|
try_with_rescue do
|
380
450
|
clear_code = %x{clear}
|
381
451
|
print clear_code or system("cls")
|
382
452
|
end
|
383
453
|
end
|
384
|
-
|
454
|
+
|
455
|
+
desc "display-session-token", "Gets the current session token"
|
456
|
+
|
457
|
+
def display_session_token
|
458
|
+
try_with_rescue_in_session do
|
459
|
+
say EnfCli::CTX.instance.auth_token.to_s
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
desc "refresh-session-token", "Refreshes the current session token"
|
464
|
+
|
465
|
+
def refresh_session_token
|
466
|
+
try_with_rescue_in_session do
|
467
|
+
# Get user and host
|
468
|
+
host = EnfCli::CTX.instance.host
|
469
|
+
user = EnfCli::CTX.instance.user
|
470
|
+
|
471
|
+
# Ask for password
|
472
|
+
say "Refreshing session token.....", :bold
|
473
|
+
password = EnfCli::ask_password()
|
474
|
+
|
475
|
+
# Authenticate
|
476
|
+
resp = EnfApi::API.instance.authenticate(host, user, password)
|
477
|
+
|
478
|
+
# update session
|
479
|
+
EnfCli::CTX.instance.session = resp[:data][0]
|
480
|
+
|
481
|
+
# display success
|
482
|
+
say "Refreshed session token!", :green
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
385
486
|
desc "#{EnfCli::NETWORK_CMD} COMMANDS", "#{EnfCli::NETWORK_CMD} commands"
|
386
487
|
subcommand EnfCli::NETWORK_CMD, EnfCli::Cmd::Xcr
|
387
|
-
|
488
|
+
|
388
489
|
desc "#{EnfCli::IAM_CMD} COMMANDS", "#{EnfCli::IAM_CMD} commands"
|
389
490
|
subcommand EnfCli::IAM_CMD, EnfCli::Cmd::Xiam
|
390
|
-
|
491
|
+
|
391
492
|
desc "#{EnfCli::FIREWALL_CMD} COMMANDS", "#{EnfCli::FIREWALL_CMD} commands"
|
392
493
|
subcommand EnfCli::FIREWALL_CMD, EnfCli::Cmd::Xfw
|
393
494
|
|
@@ -401,5 +502,4 @@ module EnfCli
|
|
401
502
|
subcommand EnfCli::DNS_CMD, EnfCli::Cmd::Xdns
|
402
503
|
end
|
403
504
|
end
|
404
|
-
|
405
505
|
end
|