コード例 #1
0
 def post(self):
     """
     Add a new policy management system. The request must provide the
      system details. used by: `katana policy add -f [yaml file]`
     """
     # Create the object and store it in the object collection
     try:
         if request.json["type"] == "test-policy":
             policy = test_policyUtils.Policy(id=request.json["id"], url=request.json["url"])
         elif request.json["type"] == "neat":
             policy = neatUtils.Policy(id=request.json["id"], url=request.json["url"])
         else:
             return "Error: Not supported Policy system type", 400
     except KeyError:
         return f"Error: Required fields: {self.req_fields}", 400
     new_uuid = str(uuid.uuid4())
     request.json["_id"] = new_uuid
     request.json["created_at"] = time.time()  # unix epoch
     try:
         new_uuid = mongoUtils.add("policy", request.json)
     except pymongo.errors.DuplicateKeyError:
         return (
             "Policy management system with id {0} already exists".format(request.json["id"]),
             400,
         )
     # Store the policy object to the mongo db
     thebytes = pickle.dumps(policy)
     obj_json = {"_id": new_uuid, "id": request.json["id"], "obj": Binary(thebytes)}
     mongoUtils.add("policy_obj", obj_json)
     return f"Created {new_uuid}", 201
コード例 #2
0
    def put(self, uuid):
        """
        Update the details of a specific policy engine system.
        used by: `katana policy update [uuid] -f [yaml file]`
        """
        data = request.json
        data["_id"] = uuid
        old_data = mongoUtils.get("policy", uuid)

        if old_data:
            data["created_at"] = old_data["created_at"]
            try:
                for entry in self.req_fields:
                    if data[entry] != old_data[entry]:
                        return "Cannot update field: " + entry, 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            else:
                mongoUtils.update("policy", uuid, data)
            return f"Modified {uuid}", 200
        else:
            # Create the object and store it in the object collection
            try:
                if request.json["type"] == "test-policy":
                    policy = test_policyUtils.Policy(id=request.json["id"],
                                                     url=request.json["url"])
                elif request.json["type"] == "neat":
                    policy = neatUtils.Policy(id=request.json["id"],
                                              url=request.json["url"])
                else:
                    return "Error: Not supported Policy system type", 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            new_uuid = str(uuid.uuid4())
            request.json["_id"] = new_uuid
            request.json["created_at"] = time.time()  # unix epoch
            try:
                new_uuid = mongoUtils.add("policy", request.json)
            except pymongo.errors.DuplicateKeyError:
                return (
                    "Policy management system with id {0} already exists".
                    format(request.json["id"]),
                    400,
                )
            # Store the policy object to the mongo db
            thebytes = pickle.dumps(policy)
            obj_json = {
                "_id": new_uuid,
                "id": request.json["id"],
                "obj": Binary(thebytes)
            }
            mongoUtils.add("policy_obj", obj_json)
            return f"Created {new_uuid}", 201