REST API - how to create ssh private key for account?

I am starting from an empty space. How do I create (via a http REST call) the ssh private key that is associated with an account?

I can create a great many things with the Octopus REST Api, but not the SSH private key.
Any help is welcome

Hi @harris.kirk!

Thanks for the great question - you can create an SSH keypair by POSTing to the /api/accounts (or the /api/{SpaceID}/accounts) enpoint with an AccountResource model. You can find the details of the model in the Swagger UI, but here’s an example

{
  "AccountType": "SshKeyPair",
  "Username": "Username",
  "PrivateKeyPassphrase": null,
  "PrivateKeyFile": {
    "HasValue": true,
    "NewValue": "BASE64_ENCODED_PRIVATEKEY"
  },
  "Name": "Name",
  "Description": "Description",
  "TenantedDeploymentParticipation": "Untenanted",
  "TenantTags": [],
  "TenantIds": [],
  "EnvironmentIds": []
}

As Octopus is API-centric, you can use the developer tools in your browser when performing any action in the web UI to see what API calls are being used.

I hope this helps!

I tried your example and ran the REST call.
When I did a health check, I got the attached error in the screenshot.
My code:
pvtkey = ‘’’-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAqru0kiCCOn7K/wAPESlqF+stZkdOl5ysDRvJULsAFMaZOwtB
etc
+AitAoGAXJWlc4SqM0j+wsom/G01tbomjCYmAragGEZkhVG3KqlLd5QkQyNgkOhC
tiJuYQ55CuEXBvtsUd1XrJe2aZLmhDprl6elTIin2oK24rogX2+bf1pqxGmvlanI
w8ABSKYO3GVtMst2O+NZgtnFxjbD5+k7a2QvkfxlLsxadUxT84E=
-----END RSA PRIVATE KEY-----’’’
data = {
“Name”: acct[‘Name’],
“AccountType”: “SshKeyPair”,
“Username”: acct[‘Username’],
“PrivateKeyFile”: {“HasValue”: True, “NewValue”: pvtkey},
“PrivateKeyPassphrase”: None,
“TenantedDeploymentParticipation”: “TenantedOrUntenanted”,
“EnvironmentIds”: env_ids,
“TenantIds”: tenant_ids,
}
response = self.executor.http_post(f"/accounts", data)
return response[“Id”]

BUT … when I then go to the UI and add the private key manually, the health check works perfectly!

Any thoughts?

Thanks for getting back to me, @harris.kirk!

You’ll want to base64 encode the private key, it looks like you’re trying to push it unencoded in your call.

Let me know how this works for you.

Best,

Hi Justin: You were right! Adding the encoding did the trick. As soon as the account was created, the icon turned green with a successful health check.

I added a screenshot of the python code that worked (import base64)

Thanks for helping me complete some key functionality and making my day!

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.