import requests
import json
import sys
from ..config import Config

def sign_certificate(publick_key, provider_id):

    config =  Config().get_config()
    url = f"http://{config['ca_factory']['url']}:{config['ca_factory']['port']}/v1/pki_int/sign/my-ca"

    headers = {'X-Vault-Token': config['ca_factory']['token']}
    data = {
        'format':'pem_bundle',
        'ttl': '43000h',
        'csr': publick_key,
        'common_name': provider_id
    }

    response = requests.request("POST", url, headers=headers, data=json.dumps(data), verify = False)
    response_payload = json.loads(response.text)

    return response_payload["data"]["certificate"]