Skip to content

Instantly share code, notes, and snippets.

@Natim
Created May 25, 2016 14:42
Show Gist options
  • Save Natim/af6a72b9bcaa428cfec29349742bbce3 to your computer and use it in GitHub Desktop.
Save Natim/af6a72b9bcaa428cfec29349742bbce3 to your computer and use it in GitHub Desktop.
A first kinto_bootstrap PoC

To use it:

python kinto_bootstrap.py -s http://localhost:8888 -a token:my-token -v
$ python kinto_bootstrap.py --help
usage: kinto_bootstrap.py [-h] [-s SERVER] [-a AUTH] [-b BUCKET] [-v] [-q]
                          [-D]

Bootstrap bucket

optional arguments:
  -h, --help            show this help message and exit
  -s SERVER, --server SERVER
                        The location of the remote server (with prefix)
  -a AUTH, --auth AUTH  BasicAuth token:my-secret
  -b BUCKET, --bucket BUCKET
                        Bucket name.
  -v, --verbose         Show all messages.
  -q, --quiet           Show only critical errors.
  -D, --debug           Show all messages, including debug messages.
import json
import logging
from kinto_client import cli_utils
logger = logging.getLogger(__file__)
NEW_VERSION = 0
with open('schema_v1.json') as f:
SCHEMA_V1 = json.load(f)['collections']
def migrate_000_001(client):
client.create_bucket()
client.create_collection(collection="certificate",
data=SCHEMA_V1['certificates']['config'])
client.create_collection(collection="addons",
data=SCHEMA_V1['addons']['config'])
client.create_collection(collection="gfx",
data=SCHEMA_V1['gfx']['config'])
client.create_collection(collection="plugins",
data=SCHEMA_V1['plugins']['config'])
# ------ To generalize in kinto_client bootstrap.py
def get_current_version(client):
try:
bucket = client.get_bucket()
return bucket['version']
except:
return NEW_VERSION
def set_current_version(client, version):
client.patch_bucket(data={'version': version})
def main(args=None):
parser = cli_utils.add_parser_options(
description="Bootstrap bucket",
default_bucket="staging",
default_collection=None,
include_collection=False)
args = parser.parse_args(args)
cli_utils.setup_logger(logger, args)
client = cli_utils.create_client_from_args(args)
current_version = get_current_version(client)
next_migration = ('migrate_%s_%s' % (
str(current_version).zfill(3),
str(current_version + 1).zfill(3)))
migrations = sorted([d for d in globals().keys()
if d.startswith('migrate_')])
try:
start_from = migrations.index(next_migration)
except ValueError:
logger.info("Nothing to migrate from %s" % current_version)
else:
for migrate in migrations[start_from:]:
logger.info("Execute %s" % migrate)
globals()[migrate](client)
if __name__ == '__main__':
main()
{
"collections": {
"addons": {
"name": "add-ons",
"synced": true,
"config": {
"displayFields": ["enabled", "blockID", "guid", "os"],
"schema": {
"definitions": {
"minVersion": {
"type": "string",
"title": "Min version",
"description": "The mininum version."
},
"maxVersion": {
"type": "string",
"title": "Max version",
"description": "The maximum version."
}
},
"title": "Add-on",
"description": "Add-on",
"type": "object",
"additionalProperties": false,
"required": [
"guid"
],
"default": {
"guid": "",
"os": "",
"prefs": [],
"versionRange": [{}]
},
"properties": {
"id": {
"type": "string",
"title": "Id",
"description": "Record id."
},
"schema": {
"type": "integer",
"title": "Schema version",
"description": "Schema version."
},
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "blocking rule is enabled.",
"default": false
},
"guid": {
"type": "string",
"title": "Add-on id",
"description": "The add-on unique identifier or a regular expression.",
"minLength": 1,
"default": ""
},
"os": {
"type": "string",
"title": "OS",
"description": "The comma-separated operating system identifiers, eg. Darwin,Linux",
"pattern": "^[^,]*(,[^,]+)*$"
},
"prefs": {
"type": "array",
"title": "Preferences",
"description": "The list of impacted preferences.",
"uniqueItems": true,
"default": [],
"items": {
"title": "Preference name",
"description": "The browser preference name, eg. browser.startup.homepage",
"type": "string",
"minLength": 1,
"default": ""
}
},
"versionRange": {
"type": "array",
"title": "Versions",
"description": "The list of impacted versions.",
"default": [],
"items": {
"type": "object",
"title": "Version range",
"description": "Version range",
"additionalProperties": false,
"default": {
"minVersion": "",
"maxVersion": "",
"severity": 0,
"vulnerabilityStatus": 0
},
"properties": {
"minVersion": {"$ref": "#/definitions/minVersion"},
"maxVersion": {"$ref": "#/definitions/maxVersion"},
"severity": {
"type": "integer",
"title": "Severity",
"description": "The severity code number.",
"enum": [
0,
1,
2,
3
],
"default": 0
},
"vulnerabilityStatus": {
"type": "integer",
"title": "Vulnerability status",
"description": "The vulnerability status code number.",
"enum": [
0,
1,
2
],
"enumNames": [
"",
"Update available",
"Update unavailable"
],
"default": 0
},
"targetApplication": {
"type": "array",
"title": "Target applications",
"description": "The list of target application information.",
"default": [],
"items": {
"type": "object",
"title": "Target application",
"description": "Target application",
"additionalProperties": false,
"required": [
"guid",
"minVersion",
"maxVersion"
],
"properties": {
"guid": {
"type": "string",
"title": "Application id",
"description": "The application unique identifier.",
"enum": [
"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
"{3550f703-e582-4d05-9a08-453d09bdfdc6}",
"{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}",
"{aa3c5121-dab2-40e2-81ca-7ea25febc110}"
],
"enumNames": [
"Firefox",
"Thunderbird",
"Seamonkey",
"Android"
]
},
"minVersion": {"$ref": "#/definitions/minVersion"},
"maxVersion": {"$ref": "#/definitions/maxVersion"}
}
}
}
}
}
},
"details": {
"type": "object",
"title": "Details",
"properties": {
"name": {"type": "string"},
"why": {"type": "string"},
"who": {"type": "string"},
"bug": {"type": "string"},
"created": {"type": "string", "format": "date-time"}
}
},
"blockID": {
"type": "string",
"title": "Internal blocklist id",
"description": "Original block id, eg. i152",
"pattern": "^i[0-9]+$"
}
}
},
"uiSchema": {
"ui:order": [
"id",
"schema",
"enabled",
"guid",
"os",
"versionRange",
"prefs",
"details",
"blockID"
],
"id": {
"ui:widget": "hidden"
},
"schema": {
"ui:widget": "hidden"
},
"blockID": {
"ui:readonly": true
},
"details": {
"why": {"ui:widget": "textarea"},
"who": {"ui:widget": "textarea"},
"created": {"type": "string", "format": "date-time"}
}
}
}
},
"certificates": {
"name": "certificates",
"synced": true,
"config": {
"displayFields": ["enabled", "blockID", "issuerName"],
"schema": {
"title": "Certificate",
"description": "A blocked certificate entry.",
"type": "object",
"additionalProperties": false,
"required": [
"issuerName",
"serialNumber"
],
"default": {
"issuerName": "",
"serialNumber": ""
},
"properties": {
"id": {
"type": "string",
"title": "Id",
"description": "Record id."
},
"schema": {
"type": "integer",
"title": "Schema version",
"description": "Schema version."
},
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "blocking rule is enabled.",
"default": false
},
"blockID": {
"type": "string",
"title": "Internal blocklist id",
"description": "Original block id, eg. c111",
"pattern": "^c[0-9]+$"
},
"issuerName": {
"type": "string",
"title": "Issuer name",
"description": "The certificate issuer name."
},
"serialNumber": {
"type": "string",
"title": "Serial number",
"description": "The certificate serial number."
},
"details": {
"type": "object",
"title": "Details",
"properties": {
"name": {"type": "string"},
"why": {"type": "string"},
"who": {"type": "string"},
"bug": {"type": "string"},
"created": {"type": "string", "format": "date-time"}
}
}
}
},
"uiSchema": {
"id": {
"ui:widget": "hidden"
},
"schema": {
"ui:widget": "hidden"
},
"blockID": {
"ui:readonly": true
},
"details": {
"why": {"ui:widget": "textarea"},
"who": {"ui:widget": "textarea"}
}
}
}
},
"gfx": {
"name": "gfx",
"synced": true,
"config": {
"displayFields": ["enabled", "blockID", "os", "vendor", "feature"],
"schema": {
"title": "Gfx",
"description": "A graphic driver blocklist entry.",
"type": "object",
"additionalProperties": false,
"required": [
"os",
"vendor",
"featureStatus"
],
"default": {
"os": "",
"vendor": "",
"hardware": "",
"devices": [],
"feature": "",
"featureStatus": "",
"driverVersion": "",
"driverVersionMax": "",
"driverVersionComparator": ""
},
"properties": {
"id": {
"type": "string",
"title": "Id",
"description": "Record id."
},
"schema": {
"type": "integer",
"title": "Schema version",
"description": "Schema version."
},
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "blocking rule is enabled.",
"default": false
},
"os": {
"type": "string",
"title": "OS",
"description": "The operating system identifier.",
"enum": [
"",
"All",
"Android",
"Darwin 9",
"Darwin 10",
"Darwin 11",
"Darwin 12",
"Darwin 13",
"Darwin 14",
"Darwin 15",
"Linux",
"WINNT 5.1",
"WINNT 5.2",
"WINNT 6.0",
"WINNT 6.1",
"WINNT 6.2",
"WINNT 6.3",
"WINNT 8.1",
"WINNT 10.0",
"Other"
]
},
"vendor": {
"type": "string",
"title": "Vendor",
"description": "A vendor identifier, eg. 0x1002"
},
"devices": {
"type": "array",
"title": "Devices",
"description": "The list of impacted devices.",
"items": {
"type": "string",
"title": "Device",
"description": "A device identifier, eg. 0x9803",
"minLength": 1
}
},
"feature": {
"type": "string",
"title": "Feature",
"description": "The type of feature impacted.",
"enum": [
"",
"CANVAS2D_ACCELERATION",
"DIRECT2D",
"DIRECT3D_9_LAYERS",
"DIRECT3D_10_LAYERS",
"DIRECT3D_10_1_LAYERS",
"DIRECT3D_11_LAYERS",
"DIRECT3D_11_ANGLE",
"HARDWARE_VIDEO_DECODING",
"OPENGL_LAYERS",
"STAGEFRIGHT",
"WEBGL_OPENGL",
"WEBGL_ANGLE",
"WEBGL_MSAA",
"WEBRTC_HW_ACCELERATION_ENCODE",
"WEBRTC_HW_ACCELERATION_DECODE",
"WEBRTC_HW_ACCELERATION"
]
},
"featureStatus": {
"type": "string",
"title": "Feature status",
"description": "The feature status.",
"enum": [
"",
"BLOCKED_DEVICE",
"BLOCKED_DRIVER_VERSION",
"BLOCKED_OS_VERSION",
"DISCOURAGED",
"STATUS_OK"
]
},
"driverVersion": {
"type": "string",
"title": "Driver version",
"description": "The driver version."
},
"driverVersionMax": {
"type": "string",
"title": "Driver version max",
"description": "The driver version."
},
"driverVersionComparator": {
"type": "string",
"title": "Driver version comparator",
"description": "The driver version comparator",
"enum": [
"",
"EQUAL",
"NOT_EQUAL",
"BETWEEN_EXCLUSIVE",
"BETWEEN_INCLUSIVE",
"BETWEEN_INCLUSIVE_START",
"LESS_THAN",
"LESS_THAN_OR_EQUAL",
"GREATER_THAN",
"GREATER_THAN_OR_EQUAL"
]
},
"hardware": {
"type": "string",
"title": "Hardware",
"description": "The graphic hardware name, eg. antares"
},
"details": {
"type": "object",
"title": "Details",
"properties": {
"name": {"type": "string"},
"why": {"type": "string"},
"who": {"type": "string"},
"bug": {"type": "string"},
"created": {"type": "string", "format": "date-time"}
}
},
"blockID": {
"type": "string",
"title": "Internal blocklist id",
"description": "Original block id, eg. g28",
"pattern": "^g[0-9]+$"
}
}
},
"uiSchema": {
"id": {
"ui:widget": "hidden"
},
"schema": {
"ui:widget": "hidden"
},
"blockID": {
"ui:readonly": true
},
"details": {
"why": {"ui:widget": "textarea"},
"who": {"ui:widget": "textarea"}
}
}
}
},
"plugins": {
"name": "plugins",
"synced": true,
"config": {
"displayFields": ["enabled", "blockID", "os", "matchName", "matchFilename", "matchDescription"],
"schema": {
"definitions": {
"minVersion": {
"type": "string",
"title": "Min version",
"description": "The mininum version."
},
"maxVersion": {
"type": "string",
"title": "Max version",
"description": "The maximum version."
}
},
"title": "Plugin",
"description": "A blocked plugin entry.",
"type": "object",
"additionalProperties": false,
"required": [
"versionRange"
],
"default": {
"os": "",
"xpcomabi": "",
"matchName": "",
"matchFilename": "",
"matchDescription": "",
"infoURL": "",
"versionRange": [{}]
},
"properties": {
"id": {
"type": "string",
"title": "Id",
"description": "Record id."
},
"schema": {
"type": "integer",
"title": "Schema version",
"description": "Schema version."
},
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "blocking rule is enabled.",
"default": false
},
"os": {
"type": "string",
"title": "OS",
"description": "The comma-separated operating system identifiers, eg. Darwin,Linux",
"pattern": "^[^,]*(,[^,]+)*$"
},
"xpcomabi": {
"type": "string",
"title": "XPCOM ABI",
"description": "The comma-separated XPCOM binary interfaces, eg. x86-gcc3,x86_64-msvc."
},
"matchName": {
"type": "string",
"title": "Name match",
"description": "A plugin name pattern to match."
},
"matchFilename": {
"type": "string",
"title": "Filename match",
"description": "A plugin filename pattern to match."
},
"matchDescription": {
"type": "string",
"title": "Description match",
"description": "A plugin description pattern to match."
},
"infoURL": {
"type": "string",
"title": "Info URL",
"description": "A HTTP URL providing related information."
},
"versionRange": {
"type": "array",
"title": "Versions",
"description": "The list of impacted versions.",
"default": [],
"items": {
"type": "object",
"title": "Version range",
"description": "Version range",
"additionalProperties": false,
"default": {
"minVersion": "",
"maxVersion": "",
"severity": 0,
"vulnerabilityStatus": 0
},
"properties": {
"minVersion": {"$ref": "#/definitions/minVersion"},
"maxVersion": {"$ref": "#/definitions/maxVersion"},
"severity": {
"type": "integer",
"title": "Severity",
"description": "The severity code number.",
"enum": [
0,
1,
2,
3
],
"default": 0
},
"vulnerabilityStatus": {
"type": "integer",
"title": "Vulnerability status",
"description": "The vulnerability status code number.",
"enum": [
0,
1,
2
],
"enumNames": [
"",
"Update available",
"Update unavailable"
],
"default": 0
},
"targetApplication": {
"type": "array",
"title": "Target applications",
"description": "The list of target application information.",
"default": [],
"items": {
"type": "object",
"title": "Target application",
"description": "Target application",
"additionalProperties": false,
"required": [
"guid",
"minVersion",
"maxVersion"
],
"properties": {
"guid": {
"type": "string",
"title": "Application id",
"description": "The application unique identifier.",
"enum": [
"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
"{3550f703-e582-4d05-9a08-453d09bdfdc6}",
"{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}",
"{aa3c5121-dab2-40e2-81ca-7ea25febc110}"
],
"enumNames": [
"Firefox",
"Thunderbird",
"Seamonkey",
"Android"
]
},
"minVersion": {"$ref": "#/definitions/minVersion"},
"maxVersion": {"$ref": "#/definitions/maxVersion"}
}
}
}
}
}
},
"details": {
"type": "object",
"title": "Details",
"properties": {
"name": {"type": "string"},
"why": {"type": "string"},
"who": {"type": "string"},
"bug": {"type": "string"},
"created": {"type": "string", "format": "date-time"}
}
},
"blockID": {
"type": "string",
"title": "Internal blocklist id",
"description": "Original block id, eg. p165",
"pattern": "^p[0-9]+$"
}
}
},
"uiSchema": {
"id": {
"ui:widget": "hidden"
},
"schema": {
"ui:widget": "hidden"
},
"blockID": {
"ui:readonly": true
},
"details": {
"why": {"ui:widget": "textarea"},
"who": {"ui:widget": "textarea"}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment