1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. 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
|
|
|
import os |
17
|
|
|
|
18
|
|
|
import jsonschema |
19
|
|
|
from oslo_config import cfg |
20
|
|
|
|
21
|
|
|
from st2common.util import schema as util_schema |
22
|
|
|
from st2common.constants.keyvalue import SYSTEM_SCOPE |
23
|
|
|
from st2common.constants.keyvalue import USER_SCOPE |
24
|
|
|
from st2common.persistence.pack import ConfigSchema |
25
|
|
|
from st2common.models.api.base import BaseAPI |
26
|
|
|
from st2common.models.db.pack import PackDB |
27
|
|
|
from st2common.models.db.pack import ConfigSchemaDB |
28
|
|
|
from st2common.models.db.pack import ConfigDB |
29
|
|
|
from st2common.exceptions.db import StackStormDBObjectNotFoundError |
30
|
|
|
|
31
|
|
|
__all__ = [ |
32
|
|
|
'PackAPI', |
33
|
|
|
'ConfigSchemaAPI', |
34
|
|
|
'ConfigAPI', |
35
|
|
|
|
36
|
|
|
'ConfigItemSetAPI' |
37
|
|
|
] |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class PackAPI(BaseAPI): |
41
|
|
|
model = PackDB |
42
|
|
|
schema = { |
43
|
|
|
'type': 'object', |
44
|
|
|
'properties': { |
45
|
|
|
'id': { |
46
|
|
|
'type': 'string', |
47
|
|
|
'default': None |
48
|
|
|
}, |
49
|
|
|
'ref': { |
50
|
|
|
'type': 'string', |
51
|
|
|
'default': None |
52
|
|
|
}, |
53
|
|
|
"uid": { |
54
|
|
|
"type": "string" |
55
|
|
|
}, |
56
|
|
|
'name': { |
57
|
|
|
'type': 'string', |
58
|
|
|
'required': True |
59
|
|
|
}, |
60
|
|
|
'description': { |
61
|
|
|
'type': 'string' |
62
|
|
|
}, |
63
|
|
|
'keywords': { |
64
|
|
|
'type': 'array', |
65
|
|
|
'items': {'type': 'string'}, |
66
|
|
|
'default': [] |
67
|
|
|
}, |
68
|
|
|
'version': { |
69
|
|
|
'type': 'string' |
70
|
|
|
}, |
71
|
|
|
'author': { |
72
|
|
|
'type': 'string' |
73
|
|
|
}, |
74
|
|
|
'email': { |
75
|
|
|
'type': 'string' |
76
|
|
|
}, |
77
|
|
|
'files': { |
78
|
|
|
'type': 'array', |
79
|
|
|
'items': {'type': 'string'}, |
80
|
|
|
'default': [] |
81
|
|
|
} |
82
|
|
|
}, |
83
|
|
|
'additionalProperties': False |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
@classmethod |
87
|
|
|
def to_model(cls, pack): |
88
|
|
|
name = pack.name |
89
|
|
|
description = pack.description |
90
|
|
|
ref = pack.ref |
91
|
|
|
keywords = getattr(pack, 'keywords', []) |
92
|
|
|
version = str(pack.version) |
93
|
|
|
author = pack.author |
94
|
|
|
email = pack.email |
95
|
|
|
files = getattr(pack, 'files', []) |
96
|
|
|
|
97
|
|
|
model = cls.model(name=name, description=description, ref=ref, keywords=keywords, |
98
|
|
|
version=version, author=author, email=email, files=files) |
99
|
|
|
return model |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
class ConfigSchemaAPI(BaseAPI): |
103
|
|
|
model = ConfigSchemaDB |
104
|
|
|
schema = { |
105
|
|
|
"title": "ConfigSchema", |
106
|
|
|
"description": "Pack config schema.", |
107
|
|
|
"type": "object", |
108
|
|
|
"properties": { |
109
|
|
|
"id": { |
110
|
|
|
"description": "The unique identifier for the config schema.", |
111
|
|
|
"type": "string" |
112
|
|
|
}, |
113
|
|
|
"pack": { |
114
|
|
|
"description": "The content pack this config schema belongs to.", |
115
|
|
|
"type": "string" |
116
|
|
|
}, |
117
|
|
|
"attributes": { |
118
|
|
|
"description": "Config schema attributes.", |
119
|
|
|
"type": "object", |
120
|
|
|
"patternProperties": { |
121
|
|
|
"^\w+$": util_schema.get_action_parameters_schema() |
|
|
|
|
122
|
|
|
}, |
123
|
|
|
"default": {} |
124
|
|
|
} |
125
|
|
|
}, |
126
|
|
|
"additionalProperties": False |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
@classmethod |
130
|
|
|
def to_model(cls, config_schema): |
131
|
|
|
pack = config_schema.pack |
132
|
|
|
attributes = config_schema.attributes |
133
|
|
|
|
134
|
|
|
model = cls.model(pack=pack, attributes=attributes) |
135
|
|
|
return model |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
class ConfigAPI(BaseAPI): |
139
|
|
|
model = ConfigDB |
140
|
|
|
schema = { |
141
|
|
|
"title": "Config", |
142
|
|
|
"description": "Pack config.", |
143
|
|
|
"type": "object", |
144
|
|
|
"properties": { |
145
|
|
|
"id": { |
146
|
|
|
"description": "The unique identifier for the config.", |
147
|
|
|
"type": "string" |
148
|
|
|
}, |
149
|
|
|
"pack": { |
150
|
|
|
"description": "The content pack this config belongs to.", |
151
|
|
|
"type": "string" |
152
|
|
|
}, |
153
|
|
|
"values": { |
154
|
|
|
"description": "Config values.", |
155
|
|
|
"type": "object", |
156
|
|
|
"default": {} |
157
|
|
|
} |
158
|
|
|
}, |
159
|
|
|
"additionalProperties": False |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
def validate(self, validate_against_schema=False): |
|
|
|
|
163
|
|
|
# Perform base API model validation against json schema |
164
|
|
|
result = super(ConfigAPI, self).validate() |
165
|
|
|
|
166
|
|
|
# Perform config values validation against the config values schema |
167
|
|
|
if validate_against_schema: |
168
|
|
|
cleaned_values = self._validate_config_values_against_schema() |
169
|
|
|
result.values = cleaned_values |
170
|
|
|
|
171
|
|
|
return result |
172
|
|
|
|
173
|
|
|
def _validate_config_values_against_schema(self): |
174
|
|
|
try: |
175
|
|
|
config_schema_db = ConfigSchema.get_by_pack(value=self.pack) |
176
|
|
|
except StackStormDBObjectNotFoundError: |
177
|
|
|
# Config schema is optional |
178
|
|
|
return |
179
|
|
|
|
180
|
|
|
# Note: We are doing optional validation so for now, we do allow additional properties |
181
|
|
|
instance = self.values or {} |
182
|
|
|
schema = config_schema_db.attributes |
183
|
|
|
schema = util_schema.get_schema_for_resource_parameters(parameters_schema=schema, |
184
|
|
|
allow_additional_properties=True) |
185
|
|
|
|
186
|
|
|
try: |
187
|
|
|
cleaned = util_schema.validate(instance=instance, schema=schema, |
188
|
|
|
cls=util_schema.CustomValidator, use_default=True, |
189
|
|
|
allow_default_none=True) |
190
|
|
|
except jsonschema.ValidationError as e: |
191
|
|
|
attribute = getattr(e, 'path', []) |
192
|
|
|
attribute = '.'.join(attribute) |
193
|
|
|
configs_path = os.path.join(cfg.CONF.system.base_path, 'configs/') |
194
|
|
|
config_path = os.path.join(configs_path, '%s.yaml' % (self.pack)) |
195
|
|
|
|
196
|
|
|
msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' % |
197
|
|
|
(attribute, self.pack, config_path, str(e))) |
198
|
|
|
raise jsonschema.ValidationError(msg) |
199
|
|
|
|
200
|
|
|
return cleaned |
201
|
|
|
|
202
|
|
|
@classmethod |
203
|
|
|
def to_model(cls, config): |
204
|
|
|
pack = config.pack |
205
|
|
|
values = config.values |
206
|
|
|
|
207
|
|
|
model = cls.model(pack=pack, values=values) |
208
|
|
|
return model |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
class ConfigItemSetAPI(BaseAPI): |
212
|
|
|
""" |
213
|
|
|
API class used with the config set API endpoint. |
214
|
|
|
""" |
215
|
|
|
model = None |
216
|
|
|
schema = { |
217
|
|
|
"title": "", |
218
|
|
|
"description": "", |
219
|
|
|
"type": "object", |
220
|
|
|
"properties": { |
221
|
|
|
"name": { |
222
|
|
|
"description": "Config item name (key)", |
223
|
|
|
"type": "string", |
224
|
|
|
"required": True |
225
|
|
|
}, |
226
|
|
|
"value": { |
227
|
|
|
"description": "Config item value.", |
228
|
|
|
"type": ["string", "number", "boolean", "array", "object"], |
229
|
|
|
"required": True |
230
|
|
|
}, |
231
|
|
|
"scope": { |
232
|
|
|
"description": "Config item scope (system / user)", |
233
|
|
|
"type": "string", |
234
|
|
|
"default": SYSTEM_SCOPE, |
235
|
|
|
"enum": [ |
236
|
|
|
SYSTEM_SCOPE, |
237
|
|
|
USER_SCOPE |
238
|
|
|
] |
239
|
|
|
}, |
240
|
|
|
"user": { |
241
|
|
|
"description": "User for user-scoped items (only available to admins).", |
242
|
|
|
"type": "string", |
243
|
|
|
"required": False, |
244
|
|
|
"default": None |
245
|
|
|
} |
246
|
|
|
}, |
247
|
|
|
"additionalProperties": False |
248
|
|
|
} |
249
|
|
|
|
Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with
r
orR
are they interpreted as regular expressions.The escape sequence that was used indicates that you might have intended to write a regular expression.
Learn more about the available escape sequences. in the Python documentation.