DoR@Hee의 끄적끄적

api 문제 ?? 본문

카테고리 없음

api 문제 ??

DoR@Hee 2020. 8. 11. 17:27

오픈스택에서 코드를 통해 ex) python을 통해서 api call을 할 때 token 발급을 통해 api 요청을 함

방법은 여러가지가 있지만 그중에서도

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "ee4dfb6e5540447cb3741905149d9b6e",
                    "password": "devstacker"
                }
            }
        },
        "scope": {
            "system": {
                "all": true
            }
        }
    }
}

위 방식으로 token을 요청해서 사용하였음, 근데 여기서 문제가 발생하는데 nova, neutron, keyston, glance에 위 방식으로 만든 토큰을 사용하여 api를 요청했을 경우 문제 없이 결과값이 반환되는것을 확인 할 수 있음

하지만 cinder, swift, trove에 대해서는 api 요청 했을 경우 에러가 나타남

http://controller:5000/v3/projects 
{'X-Auth-Token': 'gAAAAABfLaCdVSN-a4JLCxpUtwGMcIbWJaR44xnQcuz4Wui_cduAVugP_V_L5HL5cH6mEgpZHYw0MF_WR2JKgmkZBr-U8USv4gfzbKntn1mmkwnPghzr_mu729i4L5pZfMBPYl57dSgoVEZDi7tuLM3T-zwleFtOlA'}

정상적인 api 요청과 토큰

http://controller:8776/v3/7699fd0d3e5b44fe8871af7bad08df21/volumes/detail 
{'X-Auth-Token': 'gAAAAABfLaJUuts0vAH_UsYEDT8QFN2X0jR3yL75R__UKNOZqo0jUNQHPSh1GduYfgl_6KxbuME-3pPSIj4h9k76wgh-Old1dBl81vpPLOc-9GdRY5E6xUBSXQM3a4IscdkmEzpSkys9bitQQDo3yTUPSdndkDEzGg'}

{'badRequest': {'message': 'Malformed request url', 'code': 400}}

cinder, swift, trove에 대해서 api 요청을 했을 경우 나오는 결과 값

그래서 cinder list는 어떻게 api를 요청 하는지 확인

Curl
curl -g -i -X GET http://controller:8776/v3/7699fd0d3e5b44fe8871af7bad08df21/volumes/detail -H "Accept: application/json" -H "OpenStack-API-Version: volume 3.59" -H "User-Agent: python-cinderclient" -H "X-Auth-Token: {SHA256}1dc35186f7e05f9a211214a9da71e4408c254c1bc2a1b745c085bdc2091b482a"

python
import requests

headers = {
    'Accept': 'application/json',
    'OpenStack-API-Version': 'volume 3.59',
    'User-Agent': 'python-cinderclient',
    'X-Auth-Token': '{SHA256}1dc35186f7e05f9a211214a9da71e4408c254c1bc2a1b745c085bdc2091b482a',
}

response = requests.get('http://controller:8776/v3/7699fd0d3e5b44fe8871af7bad08df21/volumes/detail', headers=headers)

위와 같이 api 요청을 했을 때 정상적으로 결과물이 출력되며, 또한 저 X-Auth-Token 부분에

openstack toekn issue

명령어를 통해 나온 token을 넣어도 정상적인 결과물이 출력되는것을 확인 함

openstack token issue --debug

옵션을 통해서 토큰을 어떻게 만드는지 확인 하였고,

Using parameters {'username': 'admin', 'project_name': 'admin', 'user_domain_name': 'Default', 'auth_url': 'http://controller:5000/v3', 'password': '***', 'project_domain_name': 'Default'}

다음과 같은 파라미터를 통해서 token을 만드는것을 확인

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "ee4dfb6e5540447cb3741905149d9b6e",
                    "password": "devstacker"
                }
            }
        },
        "scope": {
            "project": {
                "id": "a6944d763bf64ee6a275f1263fae0352"
            }
        }
    }
}

그래서 위 와같은 토큰 발급 방식으로 교체를 해본 결과 제대로 api 요청을 하는 것을 확인

test code

import requests
def test_create_credentials_token(key, mode) : #토큰 생성
    print("Create Credentials Token")
    token = ""
    if mode == 1 :
        data = '{"auth":{"identity":{"methods":["password"],"password":{"user":{"id":"'+key['OS_ADMIN_ID']+'","password":"'+key['OS_PASSWORD']+'"}}},"scope":{"project":{"id":"'+key['OS_PROJECT']+'"}}}}'
    elif mode == 2 :
        data = '{"auth":{"identity":{"methods":["password"],"password":{"user":{"id":"'+key['OS_ADMIN_ID']+'","password":"'+key['OS_PASSWORD']+'"}}},"scope":{"system":{"all":true}}}}'

    res = requests.post(key['OS_AUTH_URL']+'identity/v3/auth/tokens', data=data)
    return res.headers['X-Subject-token']

def set_api(OS_TOKEN, URL) : # 서비스로 api 전송
    headers = {
        'X-Auth-Token': OS_TOKEN,
    }
    response = requests.get(URL, headers=headers)

    return response

if __name__ == "__main__":
    key = {
        "OS_ADMIN_ID" : "b76cd03ce71446e79fa47b707397e9a2", # admin id == openstack user list
        "OS_PASSWORD" : "ADMIN_PASS",# 설치 시 입력 했던 passwd
        "OS_PROJECT" : "1fb596f7cb454ec9a7d6533af8ce1826", # demo project id == openstack project list / admin project로 해도 상관 없음
        "OS_AUTH_URL" : "http://192.168.1.8/", # 설치 시 입력했던 ip ex) 192.168.1.8/
    }
    token = test_create_credentials_token(key,1)
    print("scope : project : ", token)
    result = set_api(token, key['OS_AUTH_URL']+"volume/v3/"+key['OS_PROJECT']+"/volumes/detail")
    print(result.json())

    token = test_create_credentials_token(key, 2)
    print("2 : ", token)
    result = set_api(token, key['OS_AUTH_URL']+"volume/v3/"+key['OS_PROJECT']+"/volumes/detail")
    print(result.json())

테스트 코드를 작성하고 devstack Ussuri에서 실행해본 결과

Create Credentials Token
scope : project :  gAAAAABfMlP3R8cKFu6PynJyNatvlHdKBI0EwH7OYpqIQ_Mm4pPUu5GRGZTwGrVeoG2yzU-5QlJB6aluIsEUAhQJ_5G7S1Jx1hh8V3CefFvo0oTbpi8NToh3LdgMaHEuThWOoPKkVFvJkJolVXEPjvSylcKIcfJimBdwai_cUX9e0w4c4encyI8
{'volumes': [{'id': '4bfb6455-cb31-45e7-ae3b-0e7d955662f6', 'status': 'available', 'size': 1, 'availability_zone': 'nova', 'created_at': '2020-08-10T06:22:35.000000', 'updated_at': '2020-08-10T06:22:36.000000', 'attachments': [], 'name': 'wqdqwf', 'description': '', 'volume_type': 'lvmdriver-1', 'snapshot_id': None, 'source_volid': None, 'metadata': {}, 'links': [{'rel': 'self', 'href': 'http://192.168.1.8/volume/v3/1fb596f7cb454ec9a7d6533af8ce1826/volumes/4bfb6455-cb31-45e7-ae3b-0e7d955662f6'}, {'rel': 'bookmark', 'href': 'http://192.168.1.8/volume/1fb596f7cb454ec9a7d6533af8ce1826/volumes/4bfb6455-cb31-45e7-ae3b-0e7d955662f6'}], 'user_id': 'b76cd03ce71446e79fa47b707397e9a2', 'bootable': 'false', 'encrypted': False, 'replication_status': None, 'consistencygroup_id': None, 'multiattach': False, 'migration_status': None, 'os-vol-mig-status-attr:migstat': None, 'os-vol-mig-status-attr:name_id': None, 'os-vol-tenant-attr:tenant_id': '1fb596f7cb454ec9a7d6533af8ce1826', 'os-vol-host-attr:host': 'openstack@lvmdriver-1#lvmdriver-1'}, {'id': 'b722812d-a10d-427b-900e-9776a00870eb', 'status': 'in-use', 'size': 20, 'availability_zone': 'nova', 'created_at': '2020-08-04T11:47:20.000000', 'updated_at': '2020-08-04T11:47:39.000000', 'attachments': [{'id': 'b722812d-a10d-427b-900e-9776a00870eb', 'attachment_id': 'd4e8ec13-0382-4442-b083-0d787339cb7e', 'volume_id': 'b722812d-a10d-427b-900e-9776a00870eb', 'server_id': 'a2201531-4134-4d3d-a31f-3b64e2912054', 'host_name': 'openstack', 'device': '/dev/vda', 'attached_at': '2020-08-04T11:47:39.000000'}], 'name': '', 'description': '', 'volume_type': 'lvmdriver-1', 'snapshot_id': None, 'source_volid': None, 'metadata': {}, 'links': [{'rel': 'self', 'href': 'http://192.168.1.8/volume/v3/1fb596f7cb454ec9a7d6533af8ce1826/volumes/b722812d-a10d-427b-900e-9776a00870eb'}, {'rel': 'bookmark', 'href': 'http://192.168.1.8/volume/1fb596f7cb454ec9a7d6533af8ce1826/volumes/b722812d-a10d-427b-900e-9776a00870eb'}], 'user_id': 'b76cd03ce71446e79fa47b707397e9a2', 'bootable': 'true', 'encrypted': False, 'replication_status': None, 'consistencygroup_id': None, 'multiattach': False, 'migration_status': None, 'os-vol-mig-status-attr:migstat': None, 'os-vol-mig-status-attr:name_id': None, 'os-vol-tenant-attr:tenant_id': '1fb596f7cb454ec9a7d6533af8ce1826', 'volume_image_metadata': {'signature_verified': 'False', 'image_id': 'b92204ab-733a-4738-9495-f622fb51a1fa', 'image_name': 'ubutu', 'checksum': 'b9e448f105d98dfc449ba050c7cd4f5d', 'container_format': 'bare', 'disk_format': 'raw', 'min_disk': '0', 'min_ram': '0', 'size': '356777984'}, 'os-vol-host-attr:host': 'openstack@lvmdriver-1#lvmdriver-1'}, {'id': '28b3b556-23d3-46d2-bd58-b0348dd49201', 'status': 'in-use', 'size': 1, 'availability_zone': 'nova', 'created_at': '2020-08-04T11:25:02.000000', 'updated_at': '2020-08-04T11:25:14.000000', 'attachments': [{'id': '28b3b556-23d3-46d2-bd58-b0348dd49201', 'attachment_id': 'd7f85271-f60c-44e0-8fd3-4496c24e60a7', 'volume_id': '28b3b556-23d3-46d2-bd58-b0348dd49201', 'server_id': 'b5505e7b-e556-4065-84f8-d16864a22891', 'host_name': 'openstack', 'device': '/dev/vda', 'attached_at': '2020-08-04T11:25:14.000000'}], 'name': '', 'description': '', 'volume_type': 'lvmdriver-1', 'snapshot_id': None, 'source_volid': None, 'metadata': {}, 'links': [{'rel': 'self', 'href': 'http://192.168.1.8/volume/v3/1fb596f7cb454ec9a7d6533af8ce1826/volumes/28b3b556-23d3-46d2-bd58-b0348dd49201'}, {'rel': 'bookmark', 'href': 'http://192.168.1.8/volume/1fb596f7cb454ec9a7d6533af8ce1826/volumes/28b3b556-23d3-46d2-bd58-b0348dd49201'}], 'user_id': 'b76cd03ce71446e79fa47b707397e9a2', 'bootable': 'true', 'encrypted': False, 'replication_status': None, 'consistencygroup_id': None, 'multiattach': False, 'migration_status': None, 'os-vol-mig-status-attr:migstat': None, 'os-vol-mig-status-attr:name_id': None, 'os-vol-tenant-attr:tenant_id': '1fb596f7cb454ec9a7d6533af8ce1826', 'volume_image_metadata': {'signature_verified': 'False', 'hw_rng_model': 'virtio', 'owner_specified.openstack.md5': '', 'owner_specified.openstack.sha256': '', 'owner_specified.openstack.object': 'images/cirros-0.4.0-x86_64-disk', 'image_id': 'b2c79285-15d5-49c4-8c28-02e5533f1f03', 'image_name': 'cirros-0.4.0-x86_64-disk', 'checksum': '443b7623e27ecf03dc9e01ee93f67afe', 'container_format': 'bare', 'disk_format': 'qcow2', 'min_disk': '0', 'min_ram': '0', 'size': '12716032'}, 'os-vol-host-attr:host': 'openstack@lvmdriver-1#lvmdriver-1'}]}
Create Credentials Token
2 :  gAAAAABfMlP4EkqICPEl5JhGF8qZ9WkBuCBo0ht8XB9NTBvTPNFmD6gU3rolJEVecg5byzi_3PtorWhYuPmBJ9W8QwPpV9ZQP769zeDBRdS3B9SWlLTNgTU7PRbhIFF4RDh32Rcvb3BL65pSif6-cSq7PpAxE2rbdw
{'badRequest': {'code': 400, 'message': 'Malformed request url'}}

위에는 제대로 결과값이 나오지만 아래는 실패

Comments