-
Notifications
You must be signed in to change notification settings - Fork 15
/
vagrant-softlayer-credentials
executable file
·2506 lines (1931 loc) · 122 KB
/
vagrant-softlayer-credentials
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
require 'getoptlong'
require 'rubygems'
require 'softlayer_api'
module SoftLayer
module Managers
class Credentials
def self.delete_software_credentials(softlayer_client, software_password_ids, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires software password IDs" if !software_password_ids || software_password_ids.empty?
if !options[:yes_prompt]
return false unless yes_prompt("You are deleting one or more software credentials, this cannot be undone, continue?")
end
software_password_service = softlayer_client[:Software_Component_Password]
software_password_service.deleteObjects(software_password_ids.map { |sw_pw_id| { 'id' => sw_pw_id } })
return true
end
def self.get_user_credentials_lost_password_sec_questions(softlayer_client, username, lost_password_key)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a username" if !username || username.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a lost password reset key" if !lost_password_key || lost_password_key.empty?
if softlayer_client.username != username
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
unless current_user.service.isMasterUser
raise "cannot be run as a different user than your SoftLayer user, your SoftLayer user is not a master user"
end
end
account_service = SoftLayer::Account.account_for_client(softlayer_client).service
user = get_users(softlayer_client, { :filters => { :username => [ username ] } }).first
sec_questions = user.service.getUserFromLostPasswordRequest(lost_password_key)
sec_questions = user.service.getDefaultSecurityQuestions(lost_password_key) if sec_questions.empty?
return sec_questions
end
def self.list_app_delivery_controller_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
app_delivery_controller_passwords = get_app_delivery_controller_passwords(softlayer_client, options)
software_passwords = []
if options[:show_password]
app_delivery_controller_passwords.each { |management_ip, sw_pw| software_passwords.push([ sw_pw['id'].to_s, sw_pw.username, sw_pw.password, management_ip ]) }
else
app_delivery_controller_passwords.each { |management_ip, sw_pw| software_passwords.push([ sw_pw['id'].to_s, sw_pw.username, management_ip ]) }
end
software_passwords.sort_by! { |element| element[1] }
if options[:show_password]
software_passwords.unshift([ "id", "username", "password", "management_ip" ])
else
software_passwords.unshift([ "id", "username", "management_ip" ])
end
pretty_print_table(software_passwords)
return true
end
def self.list_network_message_delivery_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
account_service = softlayer_client[:SoftLayer_Account]
net_msg_deliv_accts = options[:show_password] ? [ [ 'id', 'username', 'password', 'vendor' ] ] : [ [ 'id', 'username', 'vendor' ] ]
net_msg_deliv_object_filter = network_message_delivery_filters(options)
account_service = account_service.object_filter(net_msg_deliv_object_filter) unless net_msg_deliv_object_filter.empty?
account_service = account_service.object_mask(network_message_delivery_mask)
net_msg_deliv_accts_details = account_service.getNetworkMessageDeliveryAccounts
net_msg_deliv_accts_details.each do |net_msg_deliv_acct|
if options[:show_password]
net_msg_deliv_accts.push([ net_msg_deliv_acct['id'].to_s, net_msg_deliv_acct['username'], net_msg_deliv_acct['password'], net_msg_deliv_acct['vendor']['name'] ])
else
net_msg_deliv_accts.push([ net_msg_deliv_acct['id'].to_s, net_msg_deliv_acct['username'], net_msg_deliv_acct['vendor']['name'] ])
end
end
pretty_print_table(net_msg_deliv_accts)
return true
end
def self.list_network_vlan_firewall_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
vlan_fw_software_passwords = get_network_vlan_firewall_passwords(softlayer_client, options)
software_passwords = []
if options[:show_password]
vlan_fw_software_passwords.each { |vlan_fw, sw_pw| software_passwords.push([ sw_pw['id'].to_s, sw_pw.username, sw_pw.password, vlan_fw ]) }
else
vlan_fw_software_passwords.each { |vlan_fw, sw_pw| software_passwords.push([ sw_pw['id'].to_s, sw_pw.username, vlan_fw ]) }
end
software_passwords.sort_by! { |element| element[1] }
if options[:show_password]
software_passwords.unshift([ "id", "username", "password", "vlan firewall fqdn" ])
else
software_passwords.unshift([ "id", "username", "vlan firewall fqdn" ])
end
pretty_print_table(software_passwords)
return true
end
def self.list_software_credential_details(softlayer_client, software_password_ids, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
software_password_ids.each do |sw_pw_id|
software_password_details = [ [ 'name', 'value' ] ]
software_password_service = softlayer_client[:Software_Component_Password].object_with_id(sw_pw_id)
software_password_service = software_password_service.object_mask(software_password_mask(:"list-details"))
begin
software_password = software_password_service.getObject
rescue Exception => e
raise "failed to fine software password with id #{sw_pw_id}: #{e.message}"
end
server_type = software_password['software'].has_key?('hardware') ? 'hardware' : 'virtualGuest'
required_user = software_password['username'] == software_password['software']['softwareDescription']['requiredUser'] ? 'YES' : 'NO'
software_password_details.push([ 'id', software_password['id'].to_s ])
software_password_details.push([ 'username', software_password['username'] ])
software_password_details.push([ 'password', software_password['password'] ]) if options[:show_password]
software_password_details.push([ 'last modified', software_password['modifyDate'] ])
software_password_details.push([ 'port', software_password['port'] || "" ])
software_password_details.push([ 'notes', software_password['notes'] || "" ])
software_password_details.push([ 'required user', required_user ])
software_password_details.push([ 'description', software_password['software']['softwareDescription']['longDescription'] ])
software_password_details.push([ 'name', software_password['software']['softwareDescription']['name'] ])
software_password_details.push([ 'manufacturer', software_password['software']['softwareDescription']['manufacturer'] ])
software_password_details.push([ 'hostname', software_password['software'][server_type]['hostname'] ])
software_password_details.push([ 'domain', software_password['software'][server_type]['domain'] ])
software_password_details.push([ 'datacenter', software_password['software'][server_type]['datacenter']['name'] ])
software_password_details.push([ 'server type', software_password['software'].has_key?('hardware') ? 'Hardware' : 'Virtual Guest' ])
software_password_details.push([ 'tags', software_password['software'][server_type]['tagReferences'].collect { |tag| tag['tag']['name'] }.join(", ") ])
pretty_print_table(software_password_details)
puts if software_password_ids.length > 1
end
end
def self.list_software_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
software_passwords = get_software_passwords(softlayer_client, options)
if options[:show_password]
software_passwords.map! { |sw_pw| [ sw_pw['id'].to_s, sw_pw['software']['softwareDescription']['name'], sw_pw.username, sw_pw.password ] }
else
software_passwords.map! { |sw_pw| [ sw_pw['id'].to_s, sw_pw['software']['softwareDescription']['name'], sw_pw.username ] }
end
software_passwords.sort_by! { |element| element[1] }
if options[:show_password]
software_passwords.unshift([ "id", "software name", "username", "password" ])
else
software_passwords.unshift([ "id", "software name", "username" ])
end
pretty_print_table(software_passwords)
return true
end
def self.list_storage_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
storage_credentials_by_username = {}
storage_credentials_data = get_storage_credentials(softlayer_client, options)
storage_credentials_table = options[:show_password] ? [ [ "id", "username", "password", "storage type" ] ] : [ [ "id", "username", "storage type" ] ]
storage_credentials_data.each do |storage_service, storage_credentials|
if options[:show_password]
storage_credentials_by_username[storage_credentials[:primary_credential][:username]] = [
nil,
storage_credentials[:primary_credential][:username],
storage_credentials[:primary_credential][:password],
storage_credentials[:type]
]
else
storage_credentials_by_username[storage_credentials[:primary_credential][:username]] = [
nil,
storage_credentials[:primary_credential][:username],
storage_credentials[:type]
]
end
end
storage_credentials_data.each do |storage_service, storage_credentials|
storage_credentials[:secondary_credentials].each do |storage_credential|
if options[:show_password]
storage_credentials_by_username[storage_credential.username] = [
storage_credential['id'],
storage_credential.username,
storage_credential.password,
storage_credentials[:type].to_s
]
else
storage_credentials_by_username[storage_credential.username] = [
storage_credential['id'],
storage_credential.username,
storage_credentials[:type].to_s
]
end
end
end
storage_credentials_table.concat(storage_credentials_by_username.values)
pretty_print_table(storage_credentials_table)
return true
end
def self.list_user_credentials(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
current_user_is_master = current_user.service.isMasterUser
users = get_users(softlayer_client, options).map do |user|
if current_user_is_master
[ user['id'], [ user.last_name, ", ", user.first_name ].join, user.username, user.email, !user['additionalEmails'].empty?, !user['securityAnswers'].empty?, user.modified ]
else
[ user['id'], [ user.last_name, ", ", user.first_name ].join, user.username, user.email, !user['additionalEmails'].empty?, user.modified ]
end
end
if current_user_is_master
users.unshift([ 'id', 'name', 'username', 'email', 'additional emails', 'security questions used', 'last modified' ])
else
users.unshift([ 'id', 'name', 'username', 'email', 'additional emails', 'last modified' ])
end
pretty_print_table(users)
return true
end
def self.list_user_credentials_lost_password_sec_questions(softlayer_client, username, lost_password_key)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a username" if !username || username.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a lost password reset key" if !lost_password_key || lost_password_key.empty?
if softlayer_client.username != username
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
unless current_user.service.isMasterUser
raise "cannot be run as a different user than your SoftLayer user, your SoftLayer user is not a master user"
end
end
sec_questions = get_user_credentials_lost_password_sec_questions(softlayer_client, username, lost_password_key)
sec_questions.map! { |sec_quest| [ sec_quest['id'], sec_quest['question'] ] }
sec_questions.unshift(['id', 'security question'])
pretty_print_table(sec_questions)
return true
end
def self.list_user_credentials_sec_questions(softlayer_client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
sec_questions = softlayer_client["SoftLayer_User_Security_Question"].getAllObjects
sec_questions.map! {|sec_question| [ sec_question['id'], sec_question['question'] ]}
sec_questions.unshift([ 'id', 'security question' ])
pretty_print_table(sec_questions)
return true
end
def self.list_user_credential_details(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
current_user_is_master = current_user.service.isMasterUser
users = get_users(softlayer_client, options)
users.each do |user|
user_details = [ [ "name", "value" ] ]
sec_questions = user['securityAnswers'].map{|sec_question| [ sec_question['question']["question"], " (", sec_question['question']["id"], ")" ].join }
user_details.push([ 'id', user['id'] ])
user_details.push([ 'first name', user.first_name ])
user_details.push([ 'last name', user.last_name ])
user_details.push([ 'email', user.email ])
user_details.push([ 'additional emails', user.additional_emails.join(', ') ])
user_details.push([ 'password expires', user.password_expires ])
user_details.push([ 'last modified', user.modified ])
user_details.push([ 'username' , user.username ])
if current_user_is_master || softlayer_client.username == user.username
user_details.push([ 'api key 1', user.api_authentication_keys[0] ? user.api_authentication_keys[0]['authenticationKey'] : "" ])
user_details.push([ 'api key 2', user.api_authentication_keys[1] ? user.api_authentication_keys[1]['authenticationKey'] : "" ])
user_details.push([ 'security question 1 (id)', sec_questions[0] || "" ])
user_details.push([ 'security question 2 (id)', sec_questions[1] || "" ])
user_details.push([ 'security question 3 (id)', sec_questions[2] || "" ])
end
pretty_print_table(user_details)
puts if users.length > 1
end
return true
end
def self.send_user_credentials_lost_password_request(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if !options[:filters] || options[:filters].empty? || !options[:filters][:email] || !options[:filters][:username] || options[:filters][:username].empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires an email and username"
end
softlayer_client["SoftLayer_User_Customer"].lostPassword(options[:filters][:username], options[:filters][:email])
end
def self.set_app_delivery_controller_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential list to update, it may take a while to process, continue?")
end
if !options[:yes_prompt]
return false unless yes_prompt("You are setting one or more network application delivery controller credentials, this cannot be undone, continue?")
end
object_filters = app_delivery_controller_filters(options)
software_passwords = SoftLayer::SoftwarePassword.find_passwords_for_vlan_firewalls(:client => softlayer_client,
:application_delivery_controller_object_filter => object_filters[:app_delivery_controller],
:software_password_object_filter => object_filters[:software_password],
:software_password_object_mask => software_password_mask(:list))
SoftLayer::SoftwarePassword.update_passwords(software_passwords, password, :client => softlayer_client)
return true
end
def self.set_network_message_delivery_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential list to update, it may take a while to process, continue?")
end
if !options[:yes_prompt]
return false unless yes_prompt("You are setting one or more network message delivery credentials, this cannot be undone, continue?")
end
account_service = softlayer_client[:SoftLayer_Account]
net_msg_deliv_object_filter = network_message_delivery_filters(options)
account_service = account_service.object_filter(net_msg_deliv_object_filter) unless net_msg_deliv_object_filter.empty?
account_service = account_service.object_mask(SoftLayer::NetworkMessageDelivery.default_object_mask)
net_msg_deliv_accts = account_service.getNetworkMessageDeliveryAccounts.collect{|net_msg_deliv_acct| SoftLayer::NetworkMessageDelivery.new(softlayer_client, net_msg_deliv_acct)}
net_msg_deliv_accts.each{|net_msg_deliv_acct| net_msg_deliv_acct.password= password}
return true
end
def self.set_network_vlan_firewall_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential list to update, it may take a while to process, continue?")
end
if !options[:yes_prompt]
return false unless yes_prompt("You are setting one or more network_vlan_firewall credentials, this cannot be undone, continue?")
end
object_filters = network_vlan_firewall_filters(options)
vlan_space = (options[:filters] && options[:filters][:space]) ? options[:filters][:space] : :all
software_passwords = SoftLayer::SoftwarePassword.find_passwords_for_vlan_firewalls(:client => softlayer_client,
:software_password_object_filter => object_filters[:software_password],
:software_password_object_mask => software_password_mask(:list),
:vlan_object_filter => object_filters[:vlan],
:vlan_firewall_object_filter => object_filters[:vlan_firewall],
:vlan_space => vlan_space)
SoftLayer::SoftwarePassword.update_passwords(software_passwords, password, :client => softlayer_client)
return true
end
def self.set_software_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
if (!options[:filters] || options[:filters].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential list to update, it may take a while to process, continue?")
end
if !options[:yes_prompt]
return false unless yes_prompt("You are setting one or more software credentials, this cannot be undone, continue?")
end
software_passwords = get_software_passwords(softlayer_client, options)
SoftLayer::SoftwarePassword.update_passwords(software_passwords, password, :client => softlayer_client)
if options[:add_user_password]
add_software_credentials(softlayer_client, password, options)
end
return true
end
def self.set_storage_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
if (!options[:filters] || options[:filtesr].empty?) && (!options[:yes_prompt])
return false unless yes_prompt("No filter options have been specified to narrow credential selection list, it may take a while to process, continue?")
end
if !options[:yes_prompt]
return false unless yes_prompt("You are setting one or more storage credentials, this cannot be undone, continue?")
end
network_storage_data = get_storage(softlayer_client, options)
user_filter = (options[:filters] && options[:filters][:username]) ? options[:filters][:username] : []
network_storage_data.each do |network_storage|
network_storage.password=(password) if user_filter.empty? || user_filter.include?(network_storage.username)
network_storage.credentials.each do |credential|
if (user_filter.empty? || user_filter.include?(network_storage.username)) && network_storage.username != credential.username
network_storage.update_credential_password(credential.username, password)
end
end
end
return true
end
def self.set_user_credentials(softlayer_client, username, password, password_type, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password_type of :forum, :portal, or :vpn" if !password_type || ![ :forum, :portal, :vpn ].include?(password_type)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a username" if !username || username.empty?
if !options[:yes_prompt]
return false unless yes_prompt("You are setting user #{password_type} credentials, this cannot be undone, continue?")
end
if softlayer_client.username != username
raise "forum password change cannot be run as another user, your SoftLayer user must match the user whose forum password is being changed" if password_type == :forum
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
unless current_user.service.isMasterUser
raise "cannot be run as a different user than your SoftLayer user, your SoftLayer user is not a master user"
end
end
user = get_users(softlayer_client, { :filters => { :username => [ username ] } }).first
raise "failed to find username #{username}" if !user
case password_type
when :forum
user.service.updateForumPassword(password)
when :portal
user.service.updatePassword(password)
when :vpn
user.service.updateVpnPassword(password)
end
return true
end
def self.set_user_credentials_lost_password(softlayer_client, lost_password_key, username, password, security_question_answers, security_question_ids, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a lost password reset key" if !lost_password_key || lost_password_key.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a password" if !password || password.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a username" if !username || username.empty?
if !security_question_answers || security_question_answers.empty? || ![ 1, 3 ].include?(security_question_answers.length)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires three security question answers's that correspond to each security question id"
end
if !security_question_ids || security_question_ids.empty? || ![ 1, 3 ].include?(security_question_ids.length)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires three security question id's that correspond to each answer"
end
if security_question_answers.length != security_question_ids.length
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires equal number security question answers and security question ids"
end
if softlayer_client.username != username
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
unless current_user.service.isMasterUser
raise "cannot be run as a different user than your SoftLayer user, your SoftLayer user is not a master user"
end
end
sec_quest_answers = (0..(security_question_answers.length - 1)).to_a.map do |sec_quest_index|
{
'answer' => security_question_answers[sec_quest_index],
'questionId' => security_question_ids[sec_quest_index]
}
end
user = get_users(softlayer_client, { :filters => { :username => [ username ] } }).first
raise "failed to find username #{username}" if !user
user.service.setPasswordFromLostPasswordRequest(lost_password_key, password, sec_quest_answers)
end
def self.set_user_credentials_sec_answers(softlayer_client, username, security_question_ids, security_question_answers)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a username" if !username || username.empty?
if !security_question_answers || security_question_answers.empty? || security_question_answers.length != 3
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires three security question answers's that correspond to each security question id"
end
if !security_question_ids || security_question_ids.empty? || security_question_ids.length != 3
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires three security question id's that correspond to each answer"
end
if security_question_answers.length != security_question_ids.length
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires equal number security question answers and security question ids"
end
if softlayer_client.username != username
current_user = get_users(softlayer_client, { :filters => { :username => [ softlayer_client.username ] } }).first
raise "failed to find username #{softlayer_client.username}" if !current_user
unless current_user.service.isMasterUser
raise "cannot be run as a different user than your SoftLayer user, your SoftLayer user is not a master user"
end
end
user = get_users(softlayer_client, { :filters => { :username => [ username ] } }).first
raise "failed to find username #{username}" if !user
user.service.updateSecurityAnswers(security_question_ids.map { |sec_id| {'id' => sec_id } }, security_question_answers)
end
private
def self.add_software_credentials(softlayer_client, password, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires usernames" if !options[:filters] || !options[:filters][:username] || options[:filters][:username].empty?
hardware_types = [ :bare_metal_instance, :hardware, :network_hardware, :router ]
add_hardware_passwords = true
add_virtual_server_passwords = true
add_hardware_passwords = false if options[:filters] && options[:filters][:virtual_servers_only]
add_virtual_server_passwords = false if options[:filters] && options[:filters][:hardware_only]
if add_hardware_passwords && (options[:filters][:hardware_type] && !options[:filters][:hardware_type].empty?)
hardware_types = options[:filters][:hardware_type].map { |hardware_type| hardware_type.to_sym }
end
object_filters = software_password_filters(:add, options)
software = []
if add_hardware_passwords
hardware_types.each do |hardware_type|
software_data = SoftLayer::Software.find_software_on_hardware(:client => softlayer_client,
:hardware_object_filter => object_filters[:hardware][hardware_type],
:software_object_filter => object_filters[:software],
:software_object_mask => software_mask)
software.concat(software_data)
end
end
if add_virtual_server_passwords
software_data = SoftLayer::Software.find_software_on_virtual_servers(:client => softlayer_client,
:software_object_filter => object_filters[:software],
:software_object_mask => software_mask,
:virtual_server_object_filter => object_filters[:virtual_server])
software.concat(software_data)
end
software_passwords_template = software.map do |sw|
options[:filters][:username].collect do |username|
{ 'softwareId' => sw['id'], 'password' => password.to_s, 'username' => username } if sw['passwords'] && !sw['passwords'].map{ |pw| pw['username'] }.include?(username)
end
end
software_password_service = softlayer_client[:Software_Component_Password]
software_password_service.createObjects(software_passwords_template.flatten.compact)
end
def self.get_app_delivery_controller_passwords(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
object_filters = app_delivery_controller_filters(options)
account_service = softlayer_client[:Account]
account_service = account_service.object_filter(object_filters[:app_delivery_controller]) unless object_filters[:app_delivery_controller].empty?
account_service = account_service.object_mask("mask[id,managementIpAddress]")
app_delivery_controller_data = account_service.getApplicationDeliveryControllers
app_delivery_controller_passwords = {}
app_delivery_controller_data.collect do |app_delivery_controller|
app_delivery_controller_service = softlayer_client[:Network_Application_Delivery_Controller].object_with_id(app_delivery_controller['id'])
app_delivery_controller_service = app_delivery_controller_service.object_filter(object_filters[:software_password]) unless object_filters[:software_password].empty?
app_delivery_controller_service = app_delivery_controller_service.object_mask(SoftLayer::SoftwarePassword.default_object_mask)
app_delivery_controller_service = app_delivery_controller_service.object_mask(software_password_mask(:list))
app_delivery_controller_password_data = app_delivery_controller_service.getPassword
unless app_delivery_controller_password_data.empty?
app_delivery_controller_passwords[app_delivery_controller['managementIpAddress']] = SoftLayer::SoftwarePassword.new(softlayer_client, app_delivery_controller_password_data)
end
end
return app_delivery_controller_passwords
end
def self.get_network_vlan_firewall_passwords(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
object_filters = network_vlan_firewall_filters(options)
vlan_space = (options[:filters] && options[:filters][:space]) ? options[:filters][:space] : :all
account_service = softlayer_client[:Account]
account_service = account_service.object_filter(object_filters[:vlan]) unless object_filters[:vlan].empty?
account_service = account_service.object_mask("mask[id]")
case vlan_space
when :all
vlan_data = account_service.getNetworkVlans
when :private
vlan_data = account_service.getPrivateNetworkVlans
when :public
vlan_data = account_service.getPublicNetworkVlans
end
vlan_fw_passwords = {}
vlan_data.each do |vlan|
vlan_service = softlayer_client[:Network_Vlan].object_with_id(vlan['id'])
vlan_service = vlan_service.object_filter(object_filters[:vlan_firewall]) unless object_filters[:vlan_firewall].empty?
vlan_service = vlan_service.object_mask("mask[id,fullyQualifiedDomainName]")
vlan_fw = vlan_service.getNetworkVlanFirewall
unless vlan_fw.empty?
vlan_fw_service = softlayer_client[:Network_Vlan_Firewall].object_with_id(vlan_fw['id'])
vlan_fw_service = vlan_fw_service.object_filter(object_filters[:software_password]) unless object_filters[:software_password].empty?
vlan_fw_service = vlan_fw_service.object_mask(SoftLayer::SoftwarePassword.default_object_mask)
vlan_fw_service = vlan_fw_service.object_mask(software_password_mask(:list))
vlan_fw_password_data = vlan_fw_service.getManagementCredentials
vlan_fw_passwords[vlan_fw['fullyQualifiedDomainName']] = SoftLayer::SoftwarePassword.new(softlayer_client, vlan_fw_password_data) unless vlan_fw_password_data.empty?
end
end
return vlan_fw_passwords
end
def self.get_software_passwords(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
hardware_types = [ :bare_metal_instance, :hardware, :network_hardware, :router ]
list_hardware_passwords = true
list_virtual_server_passwords = true
list_hardware_passwords = false if options[:filters] && options[:filters][:virtual_servers_only]
list_virtual_server_passwords = false if options[:filters] && options[:filters][:hardware_only]
if list_hardware_passwords && (options[:filters][:hardware_type] && !options[:filters][:hardware_type].empty?)
hardware_types = options[:filters][:hardware_type].map { |hardware_type| hardware_type.to_sym }
end
object_filters = software_password_filters(:list, options)
software_passwords = []
if list_hardware_passwords
hardware_types.each do |hardware_type|
passwords = SoftLayer::SoftwarePassword.find_passwords_for_software_on_hardware(:client => softlayer_client,
:hardware_object_filter => object_filters[:hardware][hardware_type],
:software_object_filter => object_filters[:software],
:software_password_object_filter => object_filters[:software_password],
:software_password_object_mask => software_password_mask(:list))
software_passwords.concat(passwords)
end
end
if list_virtual_server_passwords
passwords = SoftLayer::SoftwarePassword.find_passwords_for_software_on_virtual_servers(:client => softlayer_client,
:software_object_filter => object_filters[:software],
:software_password_object_filter => object_filters[:software_password],
:software_password_object_mask => software_password_mask(:list),
:virtual_server_object_filter => object_filters[:virtual_server])
software_passwords.concat(passwords)
end
return software_passwords
end
def self.get_storage(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
if options[:filters] && options[:filters][:type] && !options[:filters][:type].empty?
storage_types = options[:filters][:type]
else
storage_types = [ :evault, :hub, :iscsi, :lockbox, :nas, :network_storage ]
end
list_hardware_storage_credentials = (options[:filters] && options[:filters][:virtual_servers_only]) ? false : true
list_virtual_server_storage_credentials = (options[:filters] && options[:filters][:hardware_only]) ? false : true
object_filters = storage_filters(options)
network_storage_data = {}
if list_hardware_storage_credentials
storage_types.each do |storage_type|
network_storage = SoftLayer::NetworkStorage.find_network_storage(:client => softlayer_client,
:network_storage_object_filter => object_filters[:hardware][storage_type],
:network_storage_type => storage_type)
network_storage.each { |net_stor| network_storage_data[net_stor['id']] = net_stor }
end
end
if list_virtual_server_storage_credentials
storage_types.each do |storage_type|
network_storage = SoftLayer::NetworkStorage.find_network_storage(:client => softlayer_client,
:network_storage_object_filter => object_filters[:virtual_server][storage_type],
:network_storage_type => storage_type)
network_storage.each { |net_stor| network_storage_data[net_stor['id']] = net_stor }
end
end
network_storage_data = network_storage_data.values
if options[:filters] && options[:filters][:username] && !options[:filters][:username].empty?
network_storage_data.delete_if do |network_storage|
!(options[:filters][:username] - [ network_storage.username, network_storage.credentials.map{|cred| cred.username} ].flatten).empty?
end
end
return network_storage_data
end
def self.get_storage_credentials(softlayer_client, options = {})
raise "#{__method__} requires a SoftLayer client but none was given" if !softlayer_client
raise "Expected an instance of SoftLayer::Client for softlayer_client" unless softlayer_client.kind_of?(SoftLayer::Client)
network_storage_data = get_storage(softlayer_client, options)
storage_credentials = {}
user_filter = (options[:filters] && options[:filters][:username]) ? options[:filters][:username] : []
network_storage_data.each do |network_storage|
storage_credentials[network_storage.service_resource.private_ip] ||= {
:primary_credential => {
:password => nil,
:username => nil
},
:secondary_credentials => [],
:type => nil
}
storage_credentials[network_storage.service_resource.private_ip][:type] = network_storage.type
if user_filter.empty? || user_filter.include?(network_storage.username)
storage_credentials[network_storage.service_resource.private_ip][:primary_credential] = {
:password => network_storage.password,
:username => network_storage.username
}
end
network_storage.credentials.each do |credential|
if user_filter.empty? || user_filter.include?(credential.username)
storage_credentials[network_storage.service_resource.private_ip][:secondary_credentials].push(credential)
end
end
end
return storage_credentials
end
def self.get_users(softlayer_client, options = {})
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::Client instance" if !softlayer_client || !softlayer_client.kind_of?(SoftLayer::Client)
account_service = SoftLayer::Account.account_for_client(softlayer_client).service
account_service = account_service.object_filter(user_filters(options)) unless user_filters(options).empty?
account_service = account_service.object_mask(user_mask)
users = account_service.getUsers.collect { |user| SoftLayer::UserCustomer.new(softlayer_client, user) unless user.empty? }.compact
return users
end
def self.object_filter_criteria_contains(object_filter, filter_path, contains_data)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::ObjectFilter" if !object_filter || !object_filter.kind_of?(SoftLayer::ObjectFilter)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires a SoftLayer::ObjectFilter filter path" if !filter_path || filter_path.empty?
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires SoftLayer::ObjectFilter data" if !contains_data || contains_data.empty?
object_filter.set_criteria_for_key_path(filter_path,
{
'operation' => 'in',
'options' => [{
'name' => 'data',
'value' => contains_data.collect{ |filter_value| filter_value.to_s }
}]
})
end
def self.pretty_print_table(table_data)
raise ArgumentError, "SoftLayer Credential Manager #{__method__} requires table data" if !table_data || table_data.empty?
table = table_data.dup
table.map { |table_row| table_row.map! {|table_col| table_col.to_s } }
max_col_widths = table.transpose.map { |col| col.group_by(&:size).max.first + 2 }
print ':' + max_col_widths.map { |col_width| '.' * col_width}.join(':' ) + ":\n:"
table[0].each_index {|col| print table[0][col].center(max_col_widths[col]) + ':' }
print "\n:" + max_col_widths.map { |col_width| '.' * col_width }.join(':') + ":\n"
table.shift
table.each do |row|
print ':'
row.each_index { |col| print row[col].center(max_col_widths[col]) + ':' }
puts
end
puts ':' + max_col_widths.map { |col_width| '.' * col_width }.join(':') + ':'
end
def self.network_message_delivery_mask()
{
"mask(SoftLayer_Network_Message_Delivery)" => [
'id',
'password',
'username',
'vendor.name'
]
}.to_sl_object_mask
end
def self.software_mask()
{
"mask(SoftLayer_Software_Component)" => [
'id',
'manufacturerActivationCode',
'manufacturerLicenseInstance',
'passwords.username'
]
}.to_sl_object_mask
end
def self.app_delivery_controller_filters(options = {})
option_to_filter_path = {
:app_delivery_controller => {
:advanced_mode => "applicationDeliveryControllers.advancedModeFlag",
:datacenter => "applicationDeliveryControllers.datacenter.name",
:management_ip => "applicationDeliveryControllers.managementIpAddress",
:name => "applicationDeliveryControllers.name",
:tags => "applicationDeliveryControllers.tagReferences.tag.name"
},
:software_password => {
:username => "password.username"
}
}
app_delivery_controller_object_filter = SoftLayer::ObjectFilter.new()
software_password_object_filter = SoftLayer::ObjectFilter.new()
app_delivery_controller_object_filter.modify { |filter| filter.accept(option_to_filter_path[:app_delivery_controller][:advanced_mode]).when_it is(true) }
if options[:filters]
option_to_filter_path[:app_delivery_controller].each do |option, filter_path|
next if option == :advanced_mode
if options[:filters][option] && !options[:filters][option].empty?
object_filter_criteria_contains(app_delivery_controller_object_filter, filter_path, options[:filters][option])
end
end
option_to_filter_path[:software_password].each do |option, filter_path|
if options[:filters][option] && !options[:filters][option].empty?
object_filter_criteria_contains(software_password_object_filter, filter_path, options[:filters][option])
end
end
end
{
:app_delivery_controller => app_delivery_controller_object_filter,
:software_password => software_password_object_filter
}
end
def self.network_message_delivery_filters(options = {})
option_to_filter_path = {
:username => 'networkMessageDeliveryAccounts.username',
:vendor => 'networkMessageDeliveryAccounts.vendor.name'
}
net_msg_deliv_object_filter = SoftLayer::ObjectFilter.new()
if options[:filters]
option_to_filter_path.each do |option, filter_path|
if options[:filters][option] && !options[:filters][option].empty?
object_filter_criteria_contains(net_msg_deliv_object_filter, filter_path, options[:filters][option])
end
end
end
return net_msg_deliv_object_filter
end
def self.network_vlan_firewall_filters(options = {})
filter_label = {
:all => 'networkVlans',
:private => 'privateNetworkVlans',
:public => 'publicNetworkVlans'