Edits history of script submission #13978 for ' Post new "incident" to verinice.veo ISMS tool (verinice.veo)'

  • python3
    import requests
    import json
    
    
    class TokenParams:
        def __init__(self, host, username, password, client_id, realm):
            self.host = host
            self.username = username
            self.password = password
            self.client_id = client_id
            self.realm = realm
    
    
    class TokenResponse:
        def __init__(self, access_token=None, **kwargs):
            self.access_token = access_token
            self.__dict__.update(kwargs)
    
    
    def get_access_token(params: TokenParams) -> str:
        url = f"https://{params.host}/auth/realms/{params.realm}/protocol/openid-connect/token"
    
        data = {
            "grant_type": "password",
            "username": params.username,
            "password": params.password,
            "client_id": params.client_id,
        }
    
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded",
            "cache-control": "no-cache",
        }
    
        response = requests.post(url, data=data, headers=headers)
    
        if response.status_code != 200:
            raise Exception(f"HTTP error! status: {response.status_code}")
    
        data = response.json()
        token_response = TokenResponse(**data)
        token = token_response.access_token
        if not token:
            raise Exception("Could not get access token")
        return token
    
    
    def main(
        incident_json: str,
        unit_id: str,
        domain_id: str,
        user="sandboxuser",
        passw=None,
        oidc_client="veo-sandbox",
        oidc_realm="verinice-sandbox",
        api_host="api.sandbox.verinice.com"
    ):
        token = get_access_token(
            TokenParams(
                host="auth.verinice.com",
                username=user,
                password=passw,
                client_id=oidc_client,
                realm=oidc_realm
            )
        )
    
        request_body = {
            "name": incident_json["Kurzbeschreibung des Vorfalls"],
            "abbreviation": incident_json["abbreviation"],
            "description": incident_json["description"],
            "owner": {
                # Update the targetUri as needed.
                "targetUri": f"https://{api_host}/veo/units/{unit_id}"
            },
            "subType": "INC_SecurityIncident",
            "status": "NEW",
            "customAspects": {
                "incident_security": incident_json["incident_security"],
                "incident_description": incident_json["incident_description"],
                "incident_generalInformation": {
                    "incident_generalInformation_document": incident_json["document"]
                }
            },
        }
    
        res = requests.post(
            f"https://{api_host}/veo/domains/{domain_id}/incidents",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {token}",
            },
            json=request_body,
        )
        return res.json()
    

    Submitted by alexander koderman474 380 days ago