cancelJob
    • PDF

    cancelJob

    • PDF

    Article Summary

    개요

    • cancelJob API 는 "Web Security Checker" 에서 제공하는 기능으로 네이버 클라우드 플랫폼 콘솔에서 사용자가 등록한 진단을 시작 전에 취소하는 API입니다.

    요청

    MethodRequest URI
    PATCHhttps://wsc.apigw.ntruss.com/api/v1/jobs/{instanceId}/cancel

    Path Variables

    파라미터필수 여부타입설명
    InstanceIdYesinteger진단 식별 번호

    요청 헤더

    IAM 인증을 위한 요청 헤더입니다.

    헤더명설명
    x-ncp-apigw-timestamp1970년 1월 1일 00:00:00 협정 세계시(UTC)부터의 경과 시간을 밀리초(Millisecond)로 나타낸 것
    API Gateway 서버와 시간 차가 5분 이상 나는 경우 유효하지 않은 요청으로 간주
    x-ncp-iam-access-key네이버 클라우드 플랫폼에서 IAM 에서 발급받은 AccessKey
    x-ncp-apigw-signature-v2요청 경로 및 헤더를 AccessKey와 맵핑되는 SecretKey로 암호화한 서명으로
    HMAC 암호화 알고리즘은 HmacSHA256 을 사용

    예시

    요청 예시

    curl -X GET "https://wsc.apigw.ntruss.com/api/v1/jobs/01234567890/cancel"
       -H "accept: application/json"
       -H "x-ncp-iam-access-key: {x-ncp-iam-access-key}"
       -H "x-ncp-apigw-timestamp: {x-ncp-apigw-timestamp}"
       -H "x-ncp-apigw-signature-v2: {x-ncp-apigw-signature-v2}"
    

    응답 예시 1 (잘못된 InstanceId 입력)

    {
        "error": {
            "errorCode": 901,
            "message": "API Call Fail"
        }
    }
    

    샘플 코드

    진단을 취소하려면 진단 검색 API와 진단 취소 API가 필요합니다. 진단 검색 API를 통해서 취소할 진단의 instanceNo의 값을 추출하고 이 값을 이용하여 취소 API를 호출해야 합니다.
    키워드 target-domain.com을 url로 검색한 샘플 코드는 다음과 같습니다. 샘플 코드의 출력된 진단 리스트 중 대기(Pending) 상태인 진단의 instanceNo 값을 복사합니다.

    $ python jobSearch.py "url target-domain.com"
    {'resources':
        {
            'current_end_page': '10',
            'current_start_page': '1',
            'record_data': [{'crawl_cnt': None,
                            'end_date': None,
                            'instanceNo': '12341234',
                            'memo': 'Wsc Sample',
                            'rescan_button': None,
                            'result_button': 'cancel',
                            'result_desc': None,
                            'scan_cnt': None,
                            'slave_data': None,
                            'start_date': '2020-05-21 22:00:00',
                            'start_url': 'http://target-domain.com',
                            'status': 'Pending'}],
            'total_cnt': 10,
            'total_page_cnt': '1'}}
    

    make_signature 함수로 시그니처를 생성하여 요청 헤드를 생성한 후 응답 코드가 200인 경우 결과를 출력하는 샘플 코드는 다음과 같습니다. 이 때 위 샘플 코드에서 복사한 instanceNo 값을 instanceId 변수에 대입합니다.

    import sys
    import os
    import hashlib
    import hmac
    import base64
    import requests
    import time
    import json
    from pprint import pprint
    
    def make_signature(method, uri, timestamp):
    
        access_key = "{accessKey}"  # access key id (from portal or sub account)
        secret_key = "{secretKey}"  # secret key (from portal or sub account)
        secret_key = bytes(secret_key, 'UTF-8')
    
        method = method
        uri = uri
    
        message = method + " " + uri + "\n" + timestamp + "\n" + access_key
        message = bytes(message, 'UTF-8')
        signingKey = base64.b64encode(hmac.new(secret_key, message, digestmod=hashlib.sha256).digest())
        return signingKey
    
    method = 'PATCH'
    instanceId = "{instanceId}" # instance id (from api)
    uri = f'/api/v1/jobs/{instanceId}/cancel'
    timestamp = str(int(time.time() * 1000))
    
    signature = make_signature(method, uri, timestamp)
    
    headers = {
        'x-ncp-apigw-signature-v2': signature.decode('utf-8'),
        'x-ncp-apigw-timestamp': timestamp,
        'x-ncp-iam-access-key': '{accessKey}' # access key id (from portal or sub 
    }
    
    response = requests.request(
        method,
        f"https://wsc.apigw.ntruss.com{uri}",
        headers=headers
    )
    
    if response.status_code == 200:
        pprint(json.loads(response.text))
    else:
        pprint(json.loads(response.text))
    
    참고

    샘플 코드는 Python3를 기반으로 작성했습니다. Java, Node.js 등 다른 언어로 작성한 샘플 코드는 API Gateway 사용 가이드의 API 호출을 참고해 주십시오.


    이 문서가 도움이 되었습니까?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.