Home > database >  Files needed to handle SSL Certificate of Server
Files needed to handle SSL Certificate of Server

Time:03-18

I have a VM. I generated SSL Certificates:

openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

After that wrote a web server using Flask with argument ssl_context:

from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/")
def index():
    return "Flask is running!"


@app.route("/data")
def some_func():
    pass

if __name__ == "__main__":
    app.run(ssl_context=("cert.pem", "key.pem"))

Now I have another VM in the same network, where I have a client app that requests some data:

import requests
import json

SERVER_URL = 'https://example.com/data'

token = "some_token"

def _send():
    query = """some_query"""

    data = {'query': query}

    headers = {'Accept': 'application/json',
               'Content-Type': 'application/json',
               'Authorization': "Bearer %s" % token
               }

    response = requests.post(SERVER_URL,
                             data=json.dumps(data).encode('utf-8'),
                             headers=headers,
                             verify=Flase)
    if response.status_code == 200:
        res = json.loads(response.text)
        return True, res, None
    return False, None, response.text


print(_send())

What I need now is to pass to verify argument path to the some certs that will help me to check SSL Certificates of Server. But which files I need to pass to verify? Same cert.pem and key.pem that I generated and passed to Flask app as ssl_contextor or I need to generated another file (If yes, how can I generate/create that file)??

CodePudding user response:

I've slightly modified your example:

  1. generate certificate with CommonName and SubjectAltName (in this example I've used domain.test):
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365 -subj /CN=domain.test -addext "subjectAltName = DNS:domain.test"
  1. the code for running the server part:
from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/")
def index():
    return "Flask is running!"


@app.route("/data", methods=["GET", "POST"])  # <-- add methods= here
def some_func():
    return jsonify({"result": "Hello World!"})  # <-- return some example data


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, ssl_context=("cert.pem", "key.pem"))
  1. the client part:
import requests
import json

SERVER_URL = "https://domain.test:5000/data"  # <-- put domain.test here

token = "some_token"


def _send():
    query = """some_query"""

    data = {"query": query}

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": "Bearer %s" % token,
    }

    response = requests.post(
        SERVER_URL,
        data=json.dumps(data).encode("utf-8"),
        headers=headers,
        verify="cert.pem",  # <-- put cert.pem here
    )

    if response.status_code == 200:
        res = json.loads(response.text)
        return True, res, None

    return False, None, response.text


print(_send())

Output without any warnings:

(True, {'result': 'Hello World!'}, None)
  • Related