-
Notifications
You must be signed in to change notification settings - Fork 31
/
createdomain.py
executable file
·111 lines (88 loc) · 3.37 KB
/
createdomain.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# createdomain.py
# December 2017
#
# create a viya domain
#
# Change History
#
# 27JAN2017 Comments added
# 27JAN2017 Added the ability to create connection domains
# 29JAN2017 Added choices to validate type of domain
# 29SEP2018 make group list comma seperated
# 01DEC2022 add token domain
# 01FEB2023 add --store option to just store the credential
#
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Create a domain
import base64
import argparse
from sharedfunctions import callrestapi
parser = argparse.ArgumentParser(description="Create a Viya Domain (password, token, connection) : Please see createcryptdomain.py to create encryption domains.")
parser.add_argument("-d","--domain", help="Enter the domain name.",required=True)
parser.add_argument("-u","--user", help="User ID for the domain.",required=False)
parser.add_argument("-p","--password", help="Password for the userid.",required=False)
parser.add_argument("-g","--groups", help="A list of groups to add to the domain. Groupid comma seperated",required=True)
parser.add_argument("-c","--desc", help="Description of the domain.",required=False)
parser.add_argument("-t","--type", help="Type of the domain: password, oauth2.0 (token) or connection (passwordless).",required=True, choices=['password','connection','oauth2.0'])
parser.add_argument("-s","--store", help="Store Credential in existing domain. (Domain Creation is skipped)", action='store_true')
args = parser.parse_args()
domain_name=args.domain
userid=args.user
pwval=args.password
groups=args.groups
desc=args.desc
type=args.type
store=args.store
if domain_name.isalnum()==False:
print("ERROR: Domain name must be alpha-numeric.")
quit()
if type=="password" and userid==None:
print("ERROR: Password domain must have a user.")
quit()
# create a python list with the groups
grouplist=groups.split(",")
# encode the password
if pwval:
cred=base64.b64encode(pwval.encode("utf-8")).decode("utf-8")
# build the rest call
reqval="/credentials/domains/"+domain_name
reqtype="put"
# build the json parameters
data = {}
data['id'] = domain_name
data['description'] = desc
data['type'] = type
# create the domain (do not run if store option is set)
if not store: callrestapi(reqval,reqtype,data=data)
# for each group passed in add their credentials to the domain
for group_id in grouplist:
print("Adding "+ group_id + " to domain " + domain_name)
reqval="/credentials/domains/"+domain_name+"/groups/"+group_id
reqtype="put"
data = {}
data['domainId'] = domain_name
data['domainType'] = type
data['identityId'] = group_id
data['identityType'] = 'group'
if userid:
data['properties']={"userId": userid}
if pwval:
data['secrets']={"password": cred}
print(data)
callrestapi(reqval,reqtype,data=data)