Authentication and authorization
To use most of the methods provided by the Cloud ML Platform Python library, you need to authorize using an access token.
Access tokens (refresh tokens) have one of the following roles that define their scope:
User. A token with this role allows you to use most of the methods available in the library.Administrator. A token with this role allows you to use all methods available in the library. For example, having an access token with this role, you can manage other tokens.
The library method reference specifies which role the token must have to call a specific method.
Depending on the access rights, a token can be obtained in different ways:
- through the VK Cloud management console (usually an access token with the
Administratorrole); - through the Cloud ML Platform library.
If a user has access to the VK Cloud management console, they can create an access token themselves and choose one of the two roles. Such a token has an unlimited validity period.
-
Go to the VK Cloud management console.
-
Select the project in which you want to create the token.
-
Go to the ML Platform → Tokens section.
-
Click Create access token.
-
In the window that opens, specify the token parameters:
- Name: can be any value.
- Role: the selected role (
UserorAdministrator).
-
Click Create.
-
In the window that opens, copy the token value and save it on your device. After the window is closed, it will be impossible to recover the token value. If it is lost, create a new token.
-
Click Done.
If a user does not have access to the VK Cloud management console, an access token can be created through the library using a registration token:
-
The owner of an access token with the
Administratorrole uses the library to create a registration token (register token) for the user. It is single-use and has a limited time to live (TTL). All access token parameters are set in the registration token: name, role, and time to live. -
Using the registration token, the user creates an access token (refresh token) through the library for authorization when calling the library methods. All access token parameters were set by the administrator in the registration token and cannot be changed.
A registration token can only be created through the Cloud ML Platform library.
-
Install the library, if this has not been done yet.
-
Create an access token with the
Administratorrole, if this has not been done yet. -
Run the Python script:
from mlplatform_client import MLPlatformfrom mlplatform_client.serializers.auth import MLPTokenTypeADMIN_REFRESH_TOKEN = "<ADMIN_TOKEN>"mlp = MLPlatform(ADMIN_REFRESH_TOKEN)register_token = mlp.create_register_token(client_name="<ACCESS_TOKEN_NAME>",access_ttl="<REGISTRATION_TOKEN_TTL>",refresh_ttl="<ACCESS_TOKEN_TTL>",token_type=<ACCESS_TOKEN_ROLE>)print(register_token)Here:
-
<ADMIN_TOKEN>: the value of an access token with theAdministratorrole. -
client_name: the name of the access token that will be created using this registration token. -
access_ttl: the time to live of the registration token, for example,2h45m30s. An expired registration token cannot be used to create an access token. -
refresh_ttl: the time to live of the access token, for example,2h. If this parameter is not specified, the access token will be valid indefinitely. -
token_type: the role of the access token:- the value
MLPTokenType.USERcorresponds to theUserrole; - the value
MLPTokenType.ADMINcorresponds to theAdministratorrole.
- the value
After running this script, the value of the created registration token will be printed.
-
-
Provide this registration token to another user so that they can use it to create an access token with the specified parameters.
-
Install the library, if this has not been done yet.
-
Make sure you have a registration token. If you do not have one, ask the owner of an access token with the
Administratorrole to create a registration token. -
Run the Python script:
from mlplatform_client import MLPlatformREGISTER_TOKEN = "<REGISTRATION_TOKEN_VALUE>"mlp = MLPlatform()refresh_token = mlp.create_refresh_token(REGISTER_TOKEN)print(refresh_token)After running this script, the value of the created access token will be printed.
The access token parameters (name, role, and time to live) are determined by the registration token.
-
Copy the token value and save it on your device.
-
Create an access token through the management console or through the Cloud ML Platform library.
Depending on which methods you plan to use, you may need an access token with a specific role. The library method reference specifies which role a token must have in order to call a particular method.
-
Create an instance of the MLPlatform class and pass the value of the obtained token to the constructor.
Example:
from mlplatform_client import MLPlatformREFRESH_TOKEN = "<ACCESS_TOKEN_VALUE>"mlp = MLPlatform(REFRESH_TOKEN)
Now you can call the methods you need using this object. The token specified when creating the object will be used for authorization when calling the method.
If authorization fails, an error message similar to this will be displayed:
-
If the token's role is insufficient to call the method:
mlplatform_client.core.exceptions.ForbiddenException: <Response [403]>. Info: {"status":403,"warnorCode":"ForbiddenException","warnorParams":{"field":"KtoTam token does not contain the following scopes: ['ml-platform:Manage']"}} -
If the token has expired:
mlplatform_client.core.exceptions.ForbiddenException: <Response [403]>. Info: {"status":403,"warnorCode":"ForbiddenException","warnorParams":{"field":"Forbidden"}}
You can only get a list of access tokens. A list of created registration tokens is not maintained.
- Go to the VK Cloud management console.
- Select the project where the tokens are located.
- Go to the ML Platform → Tokens section.
You can get information about the access token currently in use only through the Cloud ML Platform library.
-
Install the library, if this has not been done yet.
-
Authorize using an access token with the
UserorAdministratorrole, if this has not been done yet. -
Run the Python script:
from mlplatform_client import MLPlatformREFRESH_TOKEN = "<ACCESS_TOKEN>"mlp = MLPlatform(REFRESH_TOKEN)token_info=mlp.who_am_i()print(token_info)Information about the currently used access token will be printed. Example output on success:
Token my-user-token:id: 8cdc3742-XXXXtype: USERissuer_name: XXXX@example.ruexp: None
- Go to the VK Cloud management console.
- Select the project where the required token is located.
- Go to the ML Platform → Tokens section.
- Click
for the required token and select Delete.
- Click Confirm.