Have issue during curl URL for REST API used Flask url: (35) schannel: next InitializeSecurityContext failed

1

I have design web app using Flask for REST API server

for get id and key from frontend, backend will get info and do some action (only using POST method) curl command

curl -X POST -H "Content-Type:application/json" --data "{/"account_id/":/"100002/", /"access_key/":/"AKIAWDL6TY5M2INS6J7E/"}" https://192.168.172.130:443/account

However, when I am using curl command as below:

X POST -H "Content-Type:application/json" --data "{/"account_id/":/"100002/", /"access_key/":/"AKIAWDL6TY5M2INS6J7E/"}" https://192.168.172.130:443/account
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid

code design in run.py

def scan_account(_account_id:str, _access_key:str):
    # building connection to db
    mySQLDB = mysqlDBConnector()
    mySQLDB.dbConnection()

    #init record log request
    _now_time = datetime.datetime.now()
    _request_info_log:str = 'Request of account id:'+str(_account_id)+' With Access Key: '+str(_access_key)+' at: '+str(_now_time)+' direction data: incoming with action type: post request'
    mySQLDB.db_log_request_insert(_request_info_log)

    # get secret key
    _AccountID: int = _account_id
    _AccessKey: str = _access_key
    _SecretKey: str =  mySQLDB.db_get_key(_AccountID,_AccessKey)

    # init boto3 session
    _aws_session = AWS_Session(_AccessKey, _SecretKey)
    _aws_session.get_credentials()

    #init running
    _worker = Worker()
    attrs = (getattr(_worker, name) for name in dir(_worker))
    methods = filter(inspect.ismethod, attrs)
    for method in methods:
        _thread_method = threading.Thread(target=method, args=(_aws_session,))
        _thread_method.start()
        _thread_method.join()

@app.route("/account/",methods=["POST"])
def account_info():
    _account_id = request.json['account_id']
    _access_key = request.json['access_key']
    #data = {'acount_id': _account_id, 'access_key': _access_key}
    scan_account(_account_id,_access_key)
    #return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0', port='443')
python
rest
flask
asked on Stack Overflow May 29, 2019 by Little Boy • edited Jun 8, 2019 by marc_s

1 Answer

0

Ok lets get a couple of things out the way, I DO NOT suggest you use this for anything other than a local dev. Please use proper SSL.

Make sure you have pyOpenSSL properly installed.

from flask import Flask, jsonify, request, make_response

app = Flask(__name__)

@app.route("/account/",methods=["POST"])
def account_info():
    _account_id = request.json['account_id']
    _access_key = request.json['access_key']
    data = {'acount_id': _account_id, 'access_key': _access_key}
    return make_response(jsonify(data), 200)

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0', port='433', ssl_context='adhoc')

I also modified your curl to make it simpler, that and it was causing issues:

curl -X POST -H "Content-Type:application/json" --data '{"account_id":"100002", "access_key":"AKIAWDL6TY5M2INS6J7E"}' https://localhost:9443/account/ --insecure

I get the following output:

{
  "access_key": "AKIAWDL6TY5M2INS6J7E", 
  "acount_id": "100002"
}

This is how i tested it all:

docker run --rm -it -p 9443:443 python:3.7 bash -c '
pip install flask pyOpenSSL; 
curl -s https://gist.githubusercontent.com/kingbuzzman/a955b49a318eef9e76b4bf9026cd2595/raw/sample.py > sample.py; 
python sample.py'

Here is the gist source: https://gist.github.com/kingbuzzman/a955b49a318eef9e76b4bf9026cd2595

answered on Stack Overflow May 29, 2019 by Javier Buzzi • edited May 29, 2019 by Javier Buzzi

User contributions licensed under CC BY-SA 3.0