Repository: Netflix/lemur

Issue:
Uploading certificate 
Hi Team,

How can we upload / import a certificate to Lemur using APIs? I am not that much good in python scripting. Could you please guide me how can we achieve this? 



Discussion:
Please see the documentation at:
http://lemur.readthedocs.org/en/latest/developer/index.html#lemur.certificates.views.CertificatesUpload

You can use whatever language would like to talk to the API. 

Hi Kevgliss, 

Thanks for sharing the documentation, but unfortunately I am not that much familiar with python scripting. As a first step I tried to create a script for Authentication (http://lemur.readthedocs.org/en/latest/developer/index.html#post--auth-login). I am getting an output of 405. Could you please let me know how can we fix the issue with the script first and later will trying with uploading. 

**Script created**
#!/usr/bin/python
import requests
response = requests.request("POST",
                           "http://10.1.1.1/#/login",
                            data={'username': "lemur", 'password': "adsadsad"},
                            headers={'content-type': 'application/json'}
                            )
print response

**Output getting**

> > > print response
> > > Response [405]

You are using the wrong url, what you see in the browser is not the same as the api as its all javascript. I think you want something like: `http://10.1.1.1/api/1/auth/login`. 

Hi Mate,

Thanks for the update and that was informative. I am trying to develop a python script, but now the result is showing as  Response [400] and the Chrome postmaster plug in giving json format output as
 {
  "status": 400,
  "message": "Bad Request"
}

In Text Format we are getting an output as given below,
{
  "message": {
    "username": "Missing required parameter in the JSON body"
  }
}

Could you please let me know how can we fix this issue? 

The updated code was
#!/usr/bin/python
import requests
response = requests.request("POST",
                           "http://10.1.1.1/api/1/auth/login",
                            data={'username': "lemur", 'password': "adsadsad"},
                            headers={'content-type': 'application/json'}
                            )
print response

The data you send to Lemur needs to be in json format.

try:

```
#!/usr/bin/python
import json
import requests
response = requests.request("POST",
    "http://10.1.1.1/api/1/auth/login",
    data=json.dumps({'username': "lemur", 'password': "adsadsad"}),
    headers={'content-type': 'application/json'}
)
```

Hi kevgliss,

The script is seems to be providing 200 response. As per the documentation (http://lemur.readthedocs.org/en/latest/developer/index.html#post--auth-login)  we will be getting a token as output, but while executing this script. Could you please let us know how can we get that? 

Hi kevgliss,

Thanks for your guide lines I was able to create script to upload certificate to Lemur. Please find the script below. please go through it and let know if we can improve it some how. 

#!/usr/bin/python
import json
import requests
login = requests.request("POST",
    "http://10.1.1.1/api/1/auth/login",
    data=json.dumps({'username': "lemur", 'password': "admin"}),
    headers={'content-type': 'application/json'}
)
login.json()["token"]
print login.json()
Auth = {'Authorization': 'token %s' %login.json()["token"], 'content-type': 'application/json'}
rootcert = open('/home/user/scripts/spk.pem', 'rw')
privatecert = open('/home/user/scripts/private.key', 'rw')
upload = requests.request("POST",
"http://10.1.1.1/api/1/certificates/upload",
data=json.dumps({"owner": "user@domain.com", "publicCert": rootcert.read(), "privateKey": privatecert.read(), "destinations": [], "notifications": [], "replacements": [], "name": "Final Test" }),
headers=Auth
)
print upload.json()

Looks good to me! Glad you got it working. 

Above script didn't work for me.  It threw below error.

 {"reasons": {"body": "Missing data for required field."}, "message": "Validation Error."}

I had to use "body" instead of "publicCert" and "chain" instead of "intermediateCert" to make it working. 

Also in the  official lemur documentation 
(http://lemur.readthedocs.io/en/latest/developer/index.html#post--auth-login), the example for "lemur.certificates.views.CertificatesUpload" is completely misleading. 

Resolving patch:
diff --git a/lemur/certificates/views.py b/lemur/certificates/views.py
--- a/lemur/certificates/views.py
+++ b/lemur/certificates/views.py
@@ -300,12 +300,14 @@ def post(self, data=None):
 
               {
                  "owner": "joe@example.com",
-                 "publicCert": "-----BEGIN CERTIFICATE-----...",
-                 "intermediateCert": "-----BEGIN CERTIFICATE-----...",
+                 "body": "-----BEGIN CERTIFICATE-----...",
+                 "chain": "-----BEGIN CERTIFICATE-----...",
                  "privateKey": "-----BEGIN RSA PRIVATE KEY-----..."
                  "destinations": [],
                  "notifications": [],
                  "replacements": [],
+                 "roles": [],
+                 "notify": true,
                  "name": "cert1"
               }
 


Test patch: