Edits history of script submission #11467 for ' Update an incident (includes ETag/If-Match handling) (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_id: str,
        openai_description: str,
        passw: str,
        domain_id: str,
        user="sandboxuser",
        oidc_client="veo-sandbox",
        api_host="api.sandbox.verinice.com",
        realm="verinice-sandbox",
    ):
        token = get_access_token(
            TokenParams(
                host="auth.verinice.com",
                username=user,
                password=passw,
                client_id=oidc_client,
                realm=realm,
            )
        )
    
        # get ETag to prepare for PUT request
        res = requests.get(
            f"https://{api_host}/veo/domains/{domain_id}/incidents/{incident_id}",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {token}",
            },
        )
        etag = res.headers.get("ETag")
    
        # get json from response:
        existing_incident_json = res.json()
    
        # print existing_incident_json formatted:
        print(json.dumps(existing_incident_json, indent=2))
    
        cas = existing_incident_json.get("customAspects", {})
        incident_security = cas.get("incident_security", {})
        incident_security["incident_security_notificationType"] = (
            "incident_security_notificationType_earlyWarning"
        )
        cas["incident_security"] = incident_security
    
        inc_nis2pot = cas.get("incident_nis2potentialEffect", {})
        inc_nis2pot["incident_nis2potentialEffect_severityRating"] = (
            "incident_nis2potentialEffect_severityRating_significant"
        )
        inc_nis2pot["incident_nis2potentialEffect_significantIncident"] = True
        inc_nis2pot["incident_nis2potentialEffect_impactAndConsequences"] = (
            openai_description
        )
        cas["incident_nis2potentialEffect"] = inc_nis2pot
    
        res = requests.put(
            f"https://{api_host}/veo/domains/{domain_id}/incidents/{incident_id}",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {token}",
                "If-Match": etag,
            },
            json=existing_incident_json,
        )
        return res.json()
    

    Submitted by alexander koderman64 451 days ago