Search code examples
passwordsopenstackidentityopenstacksdk

how openstacksdk change current user password


I found the CLI or API request methods and they work for me, like this:

# source /etc/kolla/admin-openrc.sh
# openstack user password set --password newpsw --original-password oripsw

Or

# source /etc/kolla/admin-openrc.sh
# curl -v -s -X POST $OS_AUTH_URL/auth/tokens?nocatalog \
 -H "Content-Type: application/json" \
 -d '{ "auth": { "identity": { "methods": ["password"], \ 
 "password": {"user": {"domain": {"name": "'"$OS_USER_DOMAIN_NAME"'"}, \
 "name": "'"$OS_USERNAME"'", "password": "'"$OS_PASSWORD"'"} } }, \
 "scope": { "project": { "domain": { "name": "'"$OS_PROJECT_DOMAIN_NAME"'" }, \
 "name":  "'"$OS_PROJECT_NAME"'" } } }}'
< HTTP/1.1 201 CREATED
< Date: Mon, 18 Oct 2021 11:44:39 GMT
< Server: Apache
< Content-Length: 720
< X-Subject-Token: gAAAAABhbV4o9WvatToB4Z7dUhaNqyYqpwUt4T3wwOmnN2-YCioaSYZ-HpqdWNDvAq0pvnSe6qIuvoZXOIUjmxxUu03tWk2mp2TOJ_LTLECXOHqlQT22vqNvgJj_YTgOWbwHVlrrqbkcUWM4WDvbsD1HjM8xiEYidSNMzpw2LOHtO43cIN0nyvs
< Vary: X-Auth-Token
# export OS_TOKEN=gAAAAABhbV4o9WvatToB4Z7dUhaNqyYqpwUt4T3wwOmnN2-YCioaSYZ-HpqdWNDvAq0pvnSe6qIuvoZXOIUjmxxUu03tWk2mp2TOJ_LTLECXOHqlQT22vqNvgJj_YTgOWbwHVlrrqbkcUWM4WDvbsD1HjM8xiEYidSNMzpw2LOHtO43cIN0nyvs
# curl --header "Content-Type: application/json" --request POST --data '{"user":{"password":"123","original_password":"aaa"}}' http://10.32.17.172:5000/v3/users/e1c5cc75489f4e0cbb05c39d03b46097/password

The Change password for user method at the last of API request documentation.

But I need to achieve that by using openstacksdk in our project, I found the last method in the openstacksdk documentation -- update_user(user, **attrs), it seems the most like I look for. Unfortunately, the openstack.identity.v3.user instance doesn't have the original_password, and my code can't work:

import openstack

conn = openstack.connect(
    region_name = 'RegionOne',
    auth_url = 'http://10.32.17.172:35357/v3',
    domain_name = 'Default',
    project_name = 'admin',
    username = 'admin',
    password = '123'
)

user_args = {
    "name":"admin",
    "pasword":'aaa',  # new password
    "password_expires_at":None, 
    "links":{u'self': u'http://10.32.17.172:5000/v3/users/e1c5cc75489f4e0cbb05c39d03b46097'},
    "enabled":True, 
    "domain_id":"default",
    "original_password": "123",
}

conn.identity.update_user(user_args)

How could I implement change current user passord by openstacksdk? Thanks advance.


Solution

  • Solve by this:

    import openstack
    conn = openstack.connect(
        ...
    )
    user_args = {
        "id":"e1c5cc75489f4e0cbb05c39d03b46097"
    }
    user = conn.identity.get_user(user_args)
    user.description = "test_update_psw"
    user.password="123456"
    conn.identity.update_user(user)
    

    Update the user's info should use the user object instead of the dictionary like the question's code.

    Shame of my bad program language skill and lack of API documentation knowledge.

    And thank you very very much for one people who called tanjin.