예제 #1
0
def create_abstract(request):
    post_dict = parser.parse(unicode(request.POST.urlencode()).encode("utf-8"))

    # We are interested in values, enabled and abstract. Let"s insert empty
    # values in case some of them are null (values and abstract are never null)
    if post_dict.get("enabled") == None:
        post_dict["enabled"] = {}

    # There is no specific model for the abstract context, so we will just use
    # the ContextDefinition model. Since this context is abstract, no rendered
    # version will be saved in ContextStorage
    c_uuid = gen_context_key()
    c_data = pickle.dumps({
        "values": post_dict["values"],
        "enabled": post_dict["enabled"],
        "abstract": post_dict["abstract"]
    })

    # For debug
    # return uncache_response(HttpResponse(json.dumps(post_dict, indent=2), \
    #     content_type="text/plain"))

    ContextDefinition.objects.create(
        id=c_uuid,
        name=tou(post_dict["values"]["name"]),
        description=u"",  # TODO
        owner=request.user,
        key=u"",
        public=False,  # TODO
        data=c_data,
        checksum=0,  # TODO
        inherited=False,
        abstract=True)

    return redirect("dashboard")
예제 #2
0
def clone(request, context_id):
    global _generic_plugin

    # Fetch the entry from the db
    item = ContextDefinition.objects.get(id=context_id)
    data = {}

    # Check if the data are encrypted
    if item.key == "":
        data = pickle.loads(str(item.data))
    else:
        # Password-protected
        resp = _prompt_unencrypt_context(
            request, item,
            reverse("context_clone", kwargs={"context_id": context_id})
        )
        if "httpresp" in resp:
            return resp["httpresp"]
        elif "data" in resp:
            data = pickle.loads(resp["data"])

    # Render all of the plugins
    plugins = ContextPlugins().renderAll(
        request, data["values"], data["enabled"])

    # Append display property to every plugin, and set it to True
    for p in plugins:
        p["display"] = True

    data["values"]["name"] = _name_increment_revision(
        tou(data["values"]["name"]))

    # Render the response
    # raw = {"data":data}  # debug
    return render(
        request,
        "context/context.html",
        {
            "cernvm": _get_cernvm_config(),
            "values": data["values"],
            "id": context_id,
            "disabled": False,
            #"raw": json.dumps(raw, indent=2),
            "plugins": plugins,
            "cernvm_plugin": _generic_plugin
        }
    )
예제 #3
0
def clone(request, context_id):
    global _generic_plugin

    # Fetch the entry from the db
    item = ContextDefinition.objects.get(id=context_id)
    data = {}

    # Check if the data are encrypted
    if item.key == "":
        data = pickle.loads(str(item.data))
    else:
        # Password-protected
        resp = _prompt_unencrypt_context(
            request, item,
            reverse("context_clone", kwargs={"context_id": context_id}))
        if "httpresp" in resp:
            return resp["httpresp"]
        elif "data" in resp:
            data = pickle.loads(resp["data"])

    # Render all of the plugins
    plugins = ContextPlugins().renderAll(request, data["values"],
                                         data["enabled"])

    # Append display property to every plugin, and set it to True
    for p in plugins:
        p["display"] = True

    data["values"]["name"] = _name_increment_revision(
        tou(data["values"]["name"]))

    # Render the response
    # raw = {"data":data}  # debug
    return render(
        request,
        "context/context.html",
        {
            "cernvm": _get_cernvm_config(),
            "values": data["values"],
            "id": context_id,
            "disabled": False,
            #"raw": json.dumps(raw, indent=2),
            "plugins": plugins,
            "cernvm_plugin": _generic_plugin
        })
예제 #4
0
def clone_abstract(request, context_id):
    global _generic_plugin

    item = ContextDefinition.objects.get(id=context_id)
    data = pickle.loads(str(item.data))
    display = data["abstract"].get("display")

    p_names = ContextPlugins().get_names()
    p_dict = []

    # Display CernVM generic plugin? (It is always enabled anyway)
    generic_plugin_cp = copy.deepcopy(_generic_plugin)
    if display == None:
        generic_plugin_cp["display"] = False
    else:
        generic_plugin_cp["display"] = \
            (display.get(_generic_plugin["name"]) == 1)

    p_dict.append(generic_plugin_cp)

    for p_n in p_names:
        p = ContextPlugins().get(p_n)
        p_e = (data["enabled"].get(p_n) == 1)
        if display != None:
            p_d = (display.get(p_n) == 1)
        else:
            p_d = False
        p_dict.append({"title": p.TITLE, "name": p_n,
                       "enabled": p_e, "display": p_d})

    data["values"]["name"] = _name_increment_revision(
        tou(data["values"]["name"])
    )

    return render(
        request,
        "context/abstract.html",
        {
            "values": data["values"],
            "abstract": data["abstract"],
            "enabled": data["enabled"],
            "plugins": p_dict
        }
    )
예제 #5
0
def clone_abstract(request, context_id):
    global _generic_plugin

    item = ContextDefinition.objects.get(id=context_id)
    data = pickle.loads(str(item.data))
    display = data["abstract"].get("display")

    p_names = ContextPlugins().get_names()
    p_dict = []

    # Display CernVM generic plugin? (It is always enabled anyway)
    generic_plugin_cp = copy.deepcopy(_generic_plugin)
    if display == None:
        generic_plugin_cp["display"] = False
    else:
        generic_plugin_cp["display"] = \
            (display.get(_generic_plugin["name"]) == 1)

    p_dict.append(generic_plugin_cp)

    for p_n in p_names:
        p = ContextPlugins().get(p_n)
        p_e = (data["enabled"].get(p_n) == 1)
        if display != None:
            p_d = (display.get(p_n) == 1)
        else:
            p_d = False
        p_dict.append({
            "title": p.TITLE,
            "name": p_n,
            "enabled": p_e,
            "display": p_d
        })

    data["values"]["name"] = _name_increment_revision(
        tou(data["values"]["name"]))

    return render(
        request, "context/abstract.html", {
            "values": data["values"],
            "abstract": data["abstract"],
            "enabled": data["enabled"],
            "plugins": p_dict
        })
예제 #6
0
def create_abstract(request):
    post_dict = parser.parse(
        unicode(request.POST.urlencode()).encode("utf-8"))

    # We are interested in values, enabled and abstract. Let"s insert empty
    # values in case some of them are null (values and abstract are never null)
    if post_dict.get("enabled") == None:
        post_dict["enabled"] = {}

    # There is no specific model for the abstract context, so we will just use
    # the ContextDefinition model. Since this context is abstract, no rendered
    # version will be saved in ContextStorage
    c_uuid = gen_context_key()
    c_data = pickle.dumps({
        "values": post_dict["values"],
        "enabled": post_dict["enabled"],
        "abstract": post_dict["abstract"]
    })

    # For debug
    # return uncache_response(HttpResponse(json.dumps(post_dict, indent=2), \
    #     content_type="text/plain"))

    ContextDefinition.objects.create(
        id=c_uuid,
        name=tou(post_dict["values"]["name"]),
        description=u"",  # TODO
        owner=request.user,
        key=u"",
        public=False,  # TODO
        data=c_data,
        checksum=0,  # TODO
        inherited=False,
        abstract=True
    )

    return redirect("dashboard")
예제 #7
0
def create(request):
    post_dict = parser.parse(unicode(request.POST.urlencode()).encode("utf-8"))

    # Just for debug
    # return uncache_response( HttpResponse( json.dumps(post_dict, indent=2), content_type='text/plain' ) )

    # The values of all the plugins and the enabled plugins
    values = post_dict.get("values")
    enabled = post_dict.get("enabled")
    abstract = post_dict.get("abstract")

    # Generate a UUID for this context
    c_uuid = gen_context_key()

    # We need to generate hashes for passwords here: only on uCernVM for
    # compatibility reasons
    if "cvm_version" in values["general"] \
            and values["general"]["cvm_version"] == "uCernVM":
        if "users" in values["general"]:
            for k, v in values["general"]["users"].iteritems():
                # Don"t re-hash (useful when cloning)
                if not str(v["password"]).startswith("$6$"):
                    # rounds=5000 is used to avoid the $round=xxx$ placed into
                    # out string, see:
                    # http://pythonhosted.org/passlib/lib/passlib.hash.sha256_crypt.html
                    h = sha512_crypt.encrypt(str(v["password"]),
                                             salt_size=8,
                                             rounds=5000)
                    values["general"]["users"][k]["password"] = h

    # Collect data to save. Non-indexed data is pickled
    raw_values = {"values": values, "enabled": enabled}
    if abstract is not None:
        raw_values["abstract"] = abstract
        from_abstract = True
    else:
        from_abstract = False

    # Prepare pickled data for easy reconstruction
    # (in case somebody wants to clone a template)
    c_values = pickle.dumps(raw_values)
    c_config = ContextPlugins().renderContext(c_uuid, values, enabled)

    # Generate checksum of the configuration
    c_checksum = hashlib.sha1(c_config).hexdigest()

    # Get the possible secret key
    c_key = ""
    if ("protect" in values) and (values["protect"]):
        c_key = str(values["secret"])

    # If the content is encrypted process the data now
    if (c_key != ""):
        c_values = base64.b64encode(crypt.encrypt(c_values, c_key))
        c_config = "ENCRYPTED:" + \
            base64.b64encode(crypt.encrypt(c_config, c_key))
        c_key = salt_context_key(c_uuid, c_key)

    # Check if this is public
    c_public = False
    if ("public" in values) and (values["public"]):
        c_public = True

    # Save context definition
    ContextDefinition.objects.create(
        id=c_uuid,
        name=tou(values["name"]),
        description=tou(values["description"]),
        owner=request.user,
        key=c_key,
        public=c_public,
        data=c_values,
        checksum=c_checksum,
        inherited=False,
        abstract=False,  # only True for pure abstract contexts
        from_abstract=from_abstract)

    # Save context data (Should go to key/value store for speed-up)
    ContextStorage.objects.create(id=c_uuid, data=c_config)

    # Go to dashboard
    return redirect("dashboard")
예제 #8
0
def context_from_abstract(request, context_id, cloning=False):
    """
    Used both when creating a simple context and when cloning it
    """
    global _generic_plugin

    item = ContextDefinition.objects.get(id=context_id)

    # Check if the data are encrypted
    if item.key == "":
        data = pickle.loads(str(item.data))
    else:
        # Password-protected
        resp = _prompt_unencrypt_context(
            request, item,
            reverse("context_clone_simple", kwargs={"context_id": context_id})
        )
        if "httpresp" in resp:
            return resp["httpresp"]
        elif "data" in resp:
            data = pickle.loads(resp["data"])

    display = data["abstract"].get("display")

    # Render all of the plugins
    plugins = ContextPlugins().renderAll(request, data["values"],
                                         data["enabled"])

    # Append display property to every plugin. Defaults to False
    for p in plugins:
        if display == None:
            p["display"] = False  # default
        else:
            p["display"] = (display.get(p["id"]) == 1)

    # Display CernVM generic plugin? (It is always enabled anyway)
    generic_plugin_cp = copy.deepcopy(_generic_plugin)
    if display == None:
        generic_plugin_cp["display"] = False
    else:
        generic_plugin_cp["display"] = (
            display.get(_generic_plugin["name"]) == 1)

    # Change original name
    if cloning:
        data["values"]["name"] = _name_increment_revision(
            tou(data["values"]["name"])
        )
    else:
        data["values"]["name"] = "Context from " + data["values"]["name"]

    # Render the response
    # raw = {"data": data}  # debug
    return render(
        request,
        "context/context.html",
        {
            "cernvm": _get_cernvm_config(),
            "values": data["values"],
            "json_values": json.dumps(data["values"]),
            "disabled": False,
            "id": "",
            "parent_id": context_id,
            #"raw": json.dumps(raw, indent=2),
            "abstract_html": data["abstract"].get("html_body"),
            "from_abstract": True,
            "plugins": plugins,  # now each plugin will hold enable=True|False
            "cernvm_plugin": generic_plugin_cp
        }
    )
예제 #9
0
def create(request):
    post_dict = parser.parse(
        unicode(request.POST.urlencode()).encode("utf-8"))

    # Just for debug
    # return uncache_response( HttpResponse( json.dumps(post_dict, indent=2), content_type='text/plain' ) )

    # The values of all the plugins and the enabled plugins
    values = post_dict.get("values")
    enabled = post_dict.get("enabled")
    abstract = post_dict.get("abstract")

    # Generate a UUID for this context
    c_uuid = gen_context_key()

    # We need to generate hashes for passwords here: only on uCernVM for
    # compatibility reasons
    if "cvm_version" in values["general"] \
            and values["general"]["cvm_version"] == "uCernVM":
        if "users" in values["general"]:
            for k, v in values["general"]["users"].iteritems():
                # Don"t re-hash (useful when cloning)
                if not str(v["password"]).startswith("$6$"):
                    # rounds=5000 is used to avoid the $round=xxx$ placed into
                    # out string, see:
                    # http://pythonhosted.org/passlib/lib/passlib.hash.sha256_crypt.html
                    h = sha512_crypt.encrypt(
                        str(v["password"]), salt_size=8, rounds=5000
                    )
                    values["general"]["users"][k]["password"] = h

    # Collect data to save. Non-indexed data is pickled
    raw_values = {"values": values, "enabled": enabled}
    if abstract is not None:
        raw_values["abstract"] = abstract
        from_abstract = True
    else:
        from_abstract = False

    # Prepare pickled data for easy reconstruction
    # (in case somebody wants to clone a template)
    c_values = pickle.dumps(raw_values)
    c_config = ContextPlugins().renderContext(c_uuid, values, enabled)

    # Generate checksum of the configuration
    c_checksum = hashlib.sha1(c_config).hexdigest()

    # Get the possible secret key
    c_key = ""
    if ("protect" in values) and (values["protect"]):
        c_key = str(values["secret"])

    # If the content is encrypted process the data now
    if (c_key != ""):
        c_values = base64.b64encode(crypt.encrypt(c_values, c_key))
        c_config = "ENCRYPTED:" + \
            base64.b64encode(crypt.encrypt(c_config, c_key))
        c_key = salt_context_key(c_uuid, c_key)

    # Check if this is public
    c_public = False
    if ("public" in values) and (values["public"]):
        c_public = True

    # Save context definition
    ContextDefinition.objects.create(
        id=c_uuid,
        name=tou(values["name"]),
        description=tou(values["description"]),
        owner=request.user,
        key=c_key,
        public=c_public,
        data=c_values,
        checksum=c_checksum,
        inherited=False,
        abstract=False,  # only True for pure abstract contexts
        from_abstract=from_abstract
    )

    # Save context data (Should go to key/value store for speed-up)
    ContextStorage.objects.create(
        id=c_uuid,
        data=c_config
    )

    # Go to dashboard
    return redirect("dashboard")
예제 #10
0
def context_from_abstract(request, context_id, cloning=False):
    """
    Used both when creating a simple context and when cloning it
    """
    global _generic_plugin

    item = ContextDefinition.objects.get(id=context_id)

    # Check if the data are encrypted
    if item.key == "":
        data = pickle.loads(str(item.data))
    else:
        # Password-protected
        resp = _prompt_unencrypt_context(
            request, item,
            reverse("context_clone_simple", kwargs={"context_id": context_id}))
        if "httpresp" in resp:
            return resp["httpresp"]
        elif "data" in resp:
            data = pickle.loads(resp["data"])

    display = data["abstract"].get("display")

    # Render all of the plugins
    plugins = ContextPlugins().renderAll(request, data["values"],
                                         data["enabled"])

    # Append display property to every plugin. Defaults to False
    for p in plugins:
        if display == None:
            p["display"] = False  # default
        else:
            p["display"] = (display.get(p["id"]) == 1)

    # Display CernVM generic plugin? (It is always enabled anyway)
    generic_plugin_cp = copy.deepcopy(_generic_plugin)
    if display == None:
        generic_plugin_cp["display"] = False
    else:
        generic_plugin_cp["display"] = (display.get(
            _generic_plugin["name"]) == 1)

    # Change original name
    if cloning:
        data["values"]["name"] = _name_increment_revision(
            tou(data["values"]["name"]))
    else:
        data["values"]["name"] = "Context from " + data["values"]["name"]

    # Render the response
    # raw = {"data": data}  # debug
    return render(
        request,
        "context/context.html",
        {
            "cernvm": _get_cernvm_config(),
            "values": data["values"],
            "json_values": json.dumps(data["values"]),
            "disabled": False,
            "id": "",
            "parent_id": context_id,
            #"raw": json.dumps(raw, indent=2),
            "abstract_html": data["abstract"].get("html_body"),
            "from_abstract": True,
            "plugins": plugins,  # now each plugin will hold enable=True|False
            "cernvm_plugin": generic_plugin_cp
        })