API Keys
Zergaw CloudStack provides robust API authentication mechanisms, enabling secure and programmatic interaction with its extensive set of cloud services. This is crucial for automation, integration with custom applications, and managing your cloud infrastructure at scale.
Using API Key and Secret Key based Authentication
The primary method for programmatic access to Zergaw CloudStack APIs is through API Key and Secret Key-based Authentication. This mechanism is designed for secure, non-interactive use by scripts, applications, and automation tools.
How it Works:
-
API Key (Access Key):
- The API Key is a unique identifier generated by a user within Zergaw CloudStack.
- It serves to uniquely identify the Account (and implicitly, the user associated with generating the key) making the API request.
- The API Key is included directly in every API request as a parameter.
- Visibility: While unique, the API Key itself is not considered highly sensitive on its own as it merely identifies the caller. Its security relies heavily on the Secret Key it's paired with.
-
Secret Key (Secret Access Key):
- The Secret Key is a cryptographic key also generated by the user, paired with a specific API Key.
- Unlike the API Key, the Secret Key is highly sensitive and must be kept absolutely confidential. It is never transmitted directly in an API request.
- Its sole purpose is to generate a secure digital signature for each API call. This signature proves the authenticity of the request, demonstrating that it was indeed made by the legitimate holder of the Secret Key.
-
Signature Generation:
- Before an API call is sent, a unique signature is computed. This signature is generated by performing a cryptographic hash (e.g., HMAC-SHA1) using the Secret Key on a standardized string derived from the API command and all its parameters.
- This process ensures that even if an API request is intercepted, it cannot be tampered with or replayed without the correct Secret Key.
-
API Request Structure:
- When making an API call, the request (typically an HTTP GET or POST) includes:
- The command (e.g.,
listVirtualMachines). - All necessary parameters for the command (e.g.,
zoneid=xyz,state=Running). - The API Key (
apikey=YOUR_API_KEY). - The generated signature (
signature=YOUR_GENERATED_SIGNATURE). - Additional parameters like
response=jsonfor desired output format.
- The command (e.g.,
- When making an API call, the request (typically an HTTP GET or POST) includes:
-
Authentication and Authorization by Zergaw CloudStack:
- Upon receiving an API request, Zergaw CloudStack uses the provided API Key to retrieve the associated Secret Key stored on its side.
- It then re-computes the signature using the received command and parameters with its stored Secret Key.
- If the re-computed signature matches the
signatureprovided in the request, the request is authenticated as legitimate. - After authentication, Zergaw CloudStack performs authorization checks to ensure that the user associated with the API Key has the necessary permissions to execute the requested command.
Benefits:
- Automation: Enables scripts, CI/CD pipelines, and custom applications to programmatically interact with your cloud resources without manual intervention.
- Security: The signature-based authentication mechanism prevents request tampering and replay attacks, ensuring that only authenticated and authorized requests are processed. The Secret Key is never transmitted, mitigating exposure risks.
- Auditability: Each API Key is tied to a specific user/account, allowing for clear auditing and tracking of programmatic actions.
- Granular Control: Access policies can be applied to users who generate API keys, controlling what actions their keys can perform.
- Non-Interactive Access: Ideal for headless applications and services that need persistent access to Zergaw CloudStack without requiring a user to log in interactively.
Managing API Keys:
Users can generate, view, and revoke API Keys and their associated Secret Keys through the Zergaw CloudStack UI found under the "Accounts > Select Account > View Users > Select User > Generate New API/Secret Keys(action button at the top). It is critical to:
- Generate New Keys Regularly: For enhanced security, periodically rotate your API and Secret Keys.
- Store Secret Keys Securely: Never hardcode Secret Keys directly into application code. Utilize secure credential management systems, environment variables, or secret vaults.
- Revoke Compromised Keys Immediately: If you suspect a Secret Key has been compromised, revoke it immediately via the Zergaw CloudStack UI.
Using API Keys to interact with Zergaw CloudStack
Getting Started
To get started using the API, you should have the following:
- The API endpoint URL.
- Both the API Key and Secret Key for an Account.
- Familiarity with HTTP GET/POST and query strings.
- Knowledge of either XML or JSON.
- Knowledge of a programming language that can generate HTTP requests; for example, Java or PHP.
Making API Requests
All API requests are submitted in the form of a HTTP GET/POST with an associated command and any parameters. A request is composed of the following whether in HTTP or HTTPS:
- API Endpoint URL: This is the web services API entry point( https://cloudstack.zergaw.com/client/api)
- Command: The web services command you wish to execute, such as start an Instance or create a disk volume
- Parameters: Any additional required or optional parameters for the command
A sample API GET request looks like the following:
https://cloudstack.zergaw.com/client/api
?command=deployVirtualMachine
&serviceOfferingId=1
&diskOfferingId=1
&templateId=2
&zoneId=4
&apiKey=miVr6X7u6bN_sdahOBpjNejPgEsT35eXq-jB8CG20YI3yaxXcgpyuaIRmFI_EJTVwZ0nUkkJbPmY3y2bciKwFQ
&signature=Lxx1DM40AjcXU%2FcaiK8RAP0O1hU%3D
- The first line is the API URL.
- The second line refers to the command you wish to execute. In our example, we are attempting to deploy a fresh new Instance. It is preceded by a (?) to separate itself from the API URL.
- Lines 3-6 are the parameters for this given command. To see the command and its request parameters, please refer to https://cloudstack.zergaw.com/client/#/apidocs/. Each parameter field-value pair (field=value) is preceded by an ampersand character (&).
- Line 7 is the User API Key that uniquely identifies the Account.
- Line 8 is the signature hash created to authenticate the User Account executing the API command.
Signing and executing a request
import hashlib, hmac, base64, urllib.parse, requests
endpoint = 'https://cloudstack.zergaw.com/client/api'
api_key = 'YyxQnLs4ZRi8szDafiUhCFR1Bvs-5t2XCo6gxGFi2NlZgskCHh4YGTQ20rGhD6TIDgVxLbBQbgTkQKbL32_emA'
secret = 'csvtt1l5OTD6qLStRID-2vMh-vXsKtSLab5wMv_F_2KaPdxG98Y-ofNfOeang7M6cxYcqBpNcU3fyI6sk8rtFg'
parameters (example: listUsers)
params = {
'command': 'listUsers',
'response': 'json',
'apiKey': api_key
}
- create canonicalized query string (lowercase keys, sort)
sorted_items = sorted((k.lower(), params[k]) for k in params)
- URL encode values using urllib.parse.quote_plus
query = '&'.join(f"{k}={urllib.parse.quote_plus(str(v))}" for k, v in sorted_items)
- signature: HMAC-SHA1 of the lowercase query string (some docs require lowercasing)
sig = base64.b64encode(hmac.new(secret.encode('utf-8'),
query.lower().encode('utf-8'),
hashlib.sha1).digest()).decode()
sig = urllib.parse.quote_plus(sig)
- final URL
final = f"{endpoint}?{query}&signature={sig}"
r = requests.get(final, timeout=15, verify=True)
print(r.status_code, r.text)
Interacting with Zergaw CloudStack using python library
The easiest method of intracting with Zergaw CloudStack API is using the cs python library
- Install the library
pip install cs[async,highlight]
- Call the library on your pyhton script
from cs import CloudStack
cs = CloudStack(endpoint='https://cloudstack.example.com/client/api',
key='cloudstack api key',
secret='cloudstack api secret')
print(cs.listVirtualMachines())