Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS-143] Use case: Create an asset with our API #145

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enableGitInfo = true
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
hardWraps = true

# Everything below this are Site Params

Expand Down
57 changes: 57 additions & 0 deletions content/en/APIUseCases/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "API Use Cases"
linkTitle: "API Use Cases"
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
weight: 100
description: >
Includes practical uses for our API.
---

{{% pageinfo %}}
The pages in this book describe how you can use our API to manage your Assets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assets

We discussed the capitalization for assets this week.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The pages in this book describe how you can use our API to manage your Assets
The pages in this book describe how you can use our API to manage your assets

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we'll add more guides to this section (not only about assets or pentests), maybe we can make the sentence more generic? Something like:

The pages in this book describe how you can use our API to interact with the Cobalt platform.

The pages in this book describe how you can work with the Cobalt platform using the API.

and pentests.
{{% /pageinfo %}}

This document assumes that you can run `curl` in a command line on your system.
You can also set up the REST calls in this book in other API clients such as:

- [Postman](https://learning.postman.com/docs/getting-started/introduction/)
- [Insomnia](https://docs.insomnia.rest/)

mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
If you run `curl` from the command line, we recommend that you use the
[`jq`](https://stedolan.github.io/jq/) command line JSON processor to format output.

Without the `| jq.`, you may have output that looks like:

```
Copy link
Contributor

@ana-dashuk-cobalt ana-dashuk-cobalt Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```json

This highlights the syntax.

{"pagination":{"next_page":null,"prev_page":null},"data":[{"resource":{"id":"YOUR-ORG-ID","name":"ORG-NAME","token":"YOUR-V2-ORGANIZATION-TOKEN"},"links":{"ui":{"url":"URL-WITH-YOUR-PENTESTS"}}}]}
```

If you add a `| jq .` to the end of your REST call, you may find it easier to
read the output:

```
Copy link
Contributor

@ana-dashuk-cobalt ana-dashuk-cobalt Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```json

{
"pagination": {
"next_page": null,
"prev_page": null
},
"data": [
{
"resource": {
"id": "YOUR-ORG-ID",
"name": "ORG-NAME",
"token": "YOUR-V2-ORGANIZATION-TOKEN"
},
"links": {
"ui": {
"url": "URL-WITH-YOUR-PENTESTS"
}
}
}
]
}

```

For your convenience, we include `| jq .` in all of our sample REST calls that
provide actual output.
229 changes: 229 additions & 0 deletions content/en/APIUseCases/create_asset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
---
title: "Use Our API to Create an Asset"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option: Create an Asset via the API

Since we'll add more guides to this section, I would start the title with an action and then add "Using the API" or "via the API." At the same time, if we repeat it in each guide, this will be redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the same time, if we repeat it in each guide, this will be redundant.

Good point. Since this book already has API in the name, I'm going to limit such titles -- in this case "Create an Asset"

btw, the Google Developer Style guide suggests that we should not use via

linkTitle: "Create an Asset"
weight: 110
description: >
Use this sequence of REST calls to create an asset.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send this sequence...

Use is repeated 3 times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using "Run this sequence..."

---

{{% pageinfo %}}
Use this document with our [Cobalt API documentation](https://docs.cobalt.io) to
manage assets and pentests.
{{% /pageinfo %}}

<!-- Future task: set up variables for `YOUR-PERSONAL-API-TOKEN` and
`YOUR-V2-ORGANIZATION-TOKEN`. May support automated populating of REST calls. -->
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

This page describes how you can use our API To create an [asset](../../getting-started/glossary/#asset).
Copy link
Contributor

@ana-dashuk-cobalt ana-dashuk-cobalt Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to

We can remove this sentence, the same information is given earlier.


## Create an API Token in the Cobalt UI
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

1. Navigate to https://app.cobalt.io/settings/api-tokens.
1. If needed, sign in to the app.
1. Enter an API Token Name.
1. Select Generate New Token.
1. In the modal that appears, you should see your API Token, in the **Secret
Token** text box.
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

Save the API Token. After you exit the modal, you won't see the full token again.
If you lose it, you may have to revoke the token and start over.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save the API token. After you close the overlay, you won't see the full token again.
If you lose it, you may have to revoke the token and start again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will accept. I searched for instances of both, and "close the overlay" is 4x more popular than "exit the modal".


Substitute the API Token for `YOUR-PERSONAL-API-TOKEN` in the REST calls
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token

described on this page.

## Use the API Token to Authorize Access
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

Next, you can use the API Token to authorize access to our other endpoints. Take
the API Token that you [generated](#create-an-api-token-in-the-cobalt-ui). Substitute that value for `YOUR-PERSONAL-API-TOKEN`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next, you can use the API token to authorize in the Cobalt API. Take
the API token that you generated. Substitute that value for YOUR-PERSONAL-API-TOKEN:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, when I disable 2FA for an account, I've found REST calls to sign in and create an API token. But, those REST calls are not part of the Cobalt API. So I'm going to change this to

...

Next, you can use the API token to authorize access to the Cobalt API. Take
the API token that you generated. Substitute that value for YOUR-PERSONAL-API-TOKEN:


```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```c

With c, the syntax is highlighted properly, but nothing happens when I try curl or cURL.

curl https://api.cobalt.io/orgs \
-H "Accept: application/vnd.cobalt.v2+json" \
-H "Authorization: Bearer YOUR-PERSONAL-API-TOKEN" \
| jq .
```

{{%expand "Review sample output." %}}
You should see output similar to:

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```json

{
"pagination": {
"next_page": null,
"prev_page": null
},
"data": [
{
"resource": {
"id": "some_id",
"name": "Saxophone - Alto",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ideal world, we should have an organization to use in examples, such as Example Organization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gosh -- I really should change this to something that sounds more serious!

"token": "YOUR-V2-ORGANIZATION-TOKEN"
},
"links": {
"ui": {
"url": "https://api.cobalt.io/links/not_a_jwt_token"
}
}
}
]
}
```
{{% /expand %}}

mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
From the output, save the value for `token` as your organization token.
In our API documentation, you'll see this as `YOUR-V2-ORGANIZATION-TOKEN`.

For more information, see our API reference documentation on the
[organizations](https://docs.cobalt.io/v2/#organizations) `orgs` endpoint.
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

## Create an Asset

Now that you have the following information:

- `YOUR-PERSONAL-API-TOKEN`
- `YOUR-V2-ORGANIZATION-TOKEN`

You can create an [asset](../getting-started/glossary/#asset) with the following REST call:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(../../getting-started/glossary/#asset)


```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```c

curl -X POST "https://api.cobalt.io/assets" \
-H 'Accept: application/vnd.cobalt.v2+json' \
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
-H 'Authorization: Bearer YOUR-PERSONAL-API-TOKEN' \
-H 'Content-Type: application/vnd.cobalt.v2+json' \
-H 'Idempotency-Key: A-UNIQUE-IDENTIFIER-TO-PREVENT-UNINTENTIONAL-DUPLICATION' \
-H 'X-Org-Token: YOUR-V2-ORGANIZATION-TOKEN' \
--data '{
"title": "Test Asset",
"description": "How to describe the asset to our pentesters",
"asset_type": "web"
}' \
-v
```

For more information on each parameter, see our API Reference documentation on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API reference

how to [Create an Asset](https://docs.cobalt.io/v2/#create-an-asset).

The command we use includes a `-v`, which sets up output in verbose mode. The
command works without it; however, you would see no response from the REST call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command works without it. However, you would see no response from the REST call.


When you review the output of the REST call with the `-v`, look for the line
with `HTTP/2`. You'll see one of the following lines:
<!-- The output is associated with a `201` message, which doesn't include
results, which is why I recommend a `-v` -->

| Message | Meaning |
|------------|------------------|
| HTTP/2 201 | Asset created |
| HTTP/2 401 | No asset created. Check the value of your API Token|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API token

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking -- maybe we should PR the API reference to include this list (and rename Errors to "HTTP responses" or "HTTP response codes"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But later....

| HTTP/2 404 | No asset created. Check the value of YOUR-V2-ORGANIZATION-TOKEN|
Copy link
Contributor

@ana-dashuk-cobalt ana-dashuk-cobalt Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YOUR-V2-ORGANIZATION-TOKEN

I also get 422 when enter an invalid org token.

| HTTP/2 409 | No asset created |

<!-- Maybe this table really belongs in our API reference, next to
https://docs.cobalt.io/v2/#errors? -->
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved

## Find Your Asset ID

To add or modify or information related to your asset, you'll need the asset ID.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add or modify the information

You can find this ID with the REST call to [Get All Assets](https://docs.cobalt.io/v2/#get-all-assets):

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```c

curl -X GET "https://api.cobalt.io/assets" \
-H "Accept: application/vnd.cobalt.v2+json" \
-H "Authorization: Bearer YOUR-PERSONAL-API-TOKEN" \
-H "X-Org-Token: YOUR-V2-ORGANIZATION-TOKEN" \
| jq .
Copy link
Contributor

@ana-dashuk-cobalt ana-dashuk-cobalt Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also filter assets by their title and id by adding the following jq command:
| jq -r ".data[] | .resource | .title, .id"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa! I didn't know this!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert on this topic. I will educate myself more on jq. Some responses are too long to include them in documentation, so this can be beneficial.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a way to retrieve an ID for a specific asset by its name, but I don't know the syntax for this command. 🙁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVC. Your suggestion is an improvement over mine.

FWIW, I've set up your suggestion as a "tip", current lines 141-144

```

If you've set up more than one Asset, you may need to search through the output.
For more information about each Asset response field, see our API documentation
reference to [Get All Assets](https://docs.cobalt.io/v2/#get-all-assets).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your organization has more than 10 assets, you can add the limit parameter to the request URL to retrieve more entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might set that up as a separate note

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in current lines 136-139

(You can use limit with any number -- the link includes "5" as an example:

"If specified, returns only a specified amount of assets, e.g. https://api.cobalt.io/assets?limit=5"

{{% expand "Review sample output." %}}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```json

{
"data": {
"resource": {
"id": "YOUR-ASSET-IDENTIFIER",
"title": "Test Asset",
"description": "How to describe the asset to our pentesters",
"asset_type": "web",
"logo": null,
"attachments": []
},
"links": {
"ui": {
"url": "https://api.cobalt.io/links/<endpoint for the asset>"
}
}
}
}
```
{{% /expand %}}

mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
If you've set up more than one asset, you'll see the `id` in the same
code block as the `title`, which you may have used to [create the asset](#create-an-asset).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object as the title


Save the value of the asset `id` as `YOUR-ASSET-IDENTIFIER`. You'll use that ID,
which starts with `as_`, when updating or uploading information to your asset.

## Add or Modify Asset Details

Now that you've created an asset and have the asset ID, you can add more
information with the following REST call:


```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```c

curl -X PUT 'https://api.cobalt.io/assets/YOUR-ASSET-IDENTIFIER' \
-H 'Accept: application/vnd.cobalt.v2+json' \
-H 'Authorization: Bearer YOUR-PERSONAL-API-TOKEN' \
-H 'Content-Type: application/vnd.cobalt.v2+json' \
-H 'X-Org-Token: YOUR-V2-ORGANIZATION-TOKEN' \
--data '{
"title": "Updated title",
"description": "Updated description",
"asset_type": "web",
"size": "m",
"coverage": "standard"
mjang-cobalt marked this conversation as resolved.
Show resolved Hide resolved
}' \
-v
```

You can find optional values for `size` and `coverage` in our API reference on
how to [Update an Asset](https://docs.cobalt.io/v2/#update-an-asset).

When you review the output of the REST call with the `-v`, look for the line
with `HTTP/2`. You'll see one of the following lines:
<!-- The output is associated with a `201` message, which doesn't include
results, which is why I recommend a `-v` -->

| Message | Meaning |
|------------|------------------|
| HTTP/2 201 | Asset created |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| HTTP/2 204 | Asset updated |

| HTTP/2 401 | No asset created. Check the value of your API Token|
| HTTP/2 404 | No asset created. Check the value of YOUR-V2-ORGANIZATION-TOKEN|
| HTTP/2 409 | No asset created |


## Include an Asset Attachment

You can help our pentesters by including images or even PDFs. As noted in our
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PDF files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's time to set up another variable, to list the same attachments on this page, and in https://developer.cobalt.io/getting-started/assets/asset-description/

[Asset Documentation](../getting-started/assets/asset-description/#asset-documentation),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(../../getting-started/assets/asset-description/#asset-documentation)

you can upload several kinds of files through our UI. You can also upload the
same types of files through our API.

As an example, the following command uploads the `image.jpg` file as asset
documentation:

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```c

curl -X POST 'https://api.cobalt.io/assets/YOUR-ASSET-IDENTIFIER/attachments' \
-H 'Accept: application/vnd.cobalt.v2+json' \
-H 'Authorization: Bearer YOUR-PERSONAL-API-TOKEN' \
-H 'Content-Type: multipart/form-data' \
-H 'Idempotency-Key: A-UNIQUE-IDENTIFIER-TO-PREVENT-UNINTENTIONAL-DUPLICATION' \
-H 'X-Org-Token: YOUR-V2-ORGANIZATION-TOKEN' \
--form 'attachment=@"/path/to/image.jpg"' \
-v
```

As with [Add or Modify Asset Details](#add-or-modify-asset-details), you'll see
no output when you run a properly formatted version of this command.