Getting started with Gremlin’s API

Getting started with Gremlin’s API

Introduction

Gremlin is a simple, safe and secure service for performing Chaos Engineering experiments through a SaaS-based platform.

Prerequisites

Before you begin this tutorial, you’ll need the following:

  • A host running Ubuntu 18.04 to run the Chaos Engineering experiments on. This host will run the Gremlin agent. You need to have permissions to run commands as root with sudo on this host.
  • A Gremlin account (sign up here)

Overview

This tutorial will show you how to use Gremlin’s API

  • Step 1 - Install Gremlin
  • Step 2 - Locate API Documentation
  • Step 3 - Retrieve authorization token for the API
  • Step 4 - Use API Examples to run an experiment
  • Step 5 - Run Chaos Engineering experiment using the API
  • Step 6 - Verify Chaos Engineering experiment using the API

Step 1 - Install Gremlin

First, ssh into your host and add the gremlin repo:

ssh username@your_server_ip
echo "deb https://deb.gremlin.com/ release non-free" | sudo tee /etc/apt/sources.list.d/gremlin.list

Import the GPG key:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C81FC2F43A48B25808F9583BDFF170F324D41134 9CDB294B29A5B1E2E00C24C022E8EF3461A50EF6

Install the Gremlin client and daemon:

sudo apt-get update && sudo apt-get install -y gremlin gremlind

First, make sure you have a Gremlin account (sign up here). Then, we will grab the credentials needed to authenticate the agent we just installed. Log in to the Gremlin App using your Company name and sign-on credentials. (These were emailed to you when you signed up to start using Gremlin.) Click on the right corner circular avatar, selecting “Company Settings”.

Then, select the team you need. The ID you’re looking for is found under Configuration as “Team ID” click on your Team. Make a note of your Gremlin Secret and Gremlin Team ID.

Now, we will initialize Gremlin and follow the prompts.

gremlin init

Use the credentials you have saved from the last step.

Step 2 - Locate API Documentation

Going back to the Gremlin UI, at the bottom navigation bar you will find a link to our in-depth API documentation.

The API documentation should look like this:

Step 3 - Retrieve authorization token for the API

Before starting to play with the API, we need to authenticate our session. We will start by doing a POST request to https://api.gremlin.com/v1/users/auth

Using the terminal on your computer, run:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'email=gremlin@gremlin.com' \
    --data-urlencode 'password=P@ssw0rd1!' \
    --data-urlencode 'companyName=Chaos' \
    'https://api.gremlin.com/v1/users/auth'

The output will include your access token, it will look something like this if you’re part of more than one organization.

{
    "role": "SUPER",
    "company_name": "Chaos",
    "last_login": "2019-07-31T21:27:26.308Z",
    "company_id": "c96634ae-1100-5b91-9e99-1e40bb2fe656",
    "company_is_alfi_enabled": true,
    "expires_at": "2019-08-01T04:29:16.545Z",
    "header": "Bearer MTZhY2RjZDEtMjIxYi01YjZjLThhN2EtYTgyOWFmN2ZjYmVhOmFuYW1tZWRpbmEyMUBnbWFpbC5jb206NTZlYzNmZTYtYjNiMC0xMWU5LWJhMzctMDI0MmQyMzAyY2Yy",
    "identifier": "gremlin@gremlin.com",
    "org_id": "16acdcd1-221b-5b6c-8a7a-a829af7fcbea",
    "org_name": " Thoughtful Chaos Experiments",
    "renew_token": "3a354e60-56a2-4745-b54e-6056a2a74592",
    "token": "56ec3fe6-b3b0-11e9-ba37-0242d2302cf2",
    "tier": "Enterprise"
  },
  {
    "role": "SUPER",
    "company_name": "Chaos",
    "last_login": "2019-07-31T21:27:26.313Z",
    "company_id": "c96634ae-1100-5b91-9e99-1e40bb2fe656",
    "company_is_alfi_enabled": true,
    "expires_at": "2019-08-01T08:54:39.108Z",
    "header": "Bearer Yzk2NjM0YWUtMTEwMC01YjkxLTllOTktMWU0MGJiMmZlNjU2OmFuYW1tZWRpbmEyMUBnbWFpbC5jb206Njk4MjhjNjAtYjNkNS0xMWU5LWI2OWUtMDI0MmY2MjBhODZi",
    "identifier": "gremlin@gremlin.com",
    "org_id": "c96634ae-1100-5b91-9e99-1e40bb2fe656",
    "org_name": "Break Things on Purpose",
    "renew_token": "fe581c56-22b1-4943-981c-5622b1c94303",
    "token": "69828c60-b3d5-11e9-b69e-0242f620a86b",
    "tier": "Enterprise"
  }

If you have MFA enabled, include the token from your password authenticator in your call to /users/auth/mfa/auth. You can see a sample request with MFA enabled below.

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'email=gremlin@gremlin.com' \
    --data-urlencode 'password=changeit' \
    --data-urlencode 'token=000000' \
    'https://api.gremlin.com/v1/users/auth/mfa/auth?getCompanySession=true' 

Note the Bearer token from the value of "header", we can make than environment variable using export.

export bearertoken="Bearer Yzk2NjM0YWUtMTEwMC01YjkxLTllOTktMWU0MGJiMmZlNjU2OmFuYW1tZWRpbmEyMUBnbWFpbC5jb206Njk4MjhjNjAtYjNkNS0xMWU5LWI2OWUtMDI0MmY2MjBhODZi"

Step 4 - Use API Examples to run an experiment

You can also find the Bearer token on the Gremlin Attack Page using Gremlin Examples.

Going to the Gremlin's Attack Page, select New Attack, Select the Host you want by using the instance-id.

Then, select the Resource Attacks and choose CPU Attack that runs for 120 seconds (2 mins), it targets 100% of all cores.

Before unleashing the attack, scroll down and select "Gremlin API Examples" and find the CURL commands, endpoints, request headers, and request bodies.


Press "copy to clipboard" on the CURL command and run it on your terminal, you will be returned the execution ID.

You can then go to attacks to verify that the attack is running.

Step 5 - Run Chaos Engineering experiment using the API

Using the $bearertoken we just created, let’s start our first Chaos Engineering attack. We have only installed Gremlin on one host and we will set the target as random. This experiment will be a CPU Attack that runs for 120 seconds (2 mins), it targets 100% of all cores. Make sure to switch out the teamID with your own.

curl -X POST \
    --header "Content-Type: application/json" \
    --header "Authorization: $bearertoken" \
    https://api.gremlin.com/v1/attacks/new?teamId=c96634ae-1100-5b91-9e99-1e40bb2fe656 \
    --data '
    {
        "command": { "type": "cpu", "args": ["-c", "1", "--length", "120"] },
        "target": { "type": "Random" }
    }'

The API will return the execution ID like this:

443bf563-21c7-11ea-b4dc-024221b6d1e6

Step 6 - Verify Chaos Engineering experiment using the API

We can now use the Gremlin API to get the attack details, make sure to edit your teamID and execution ID:

 curl -X GET "https://api.gremlin.com/v1/attacks/443bf563-21c7-11ea-b4dc-024221b6d1e6?teamId=c96634ae-1100-5b91-9e99-1e40bb2fe656" \
    --header "Authorization: $bearertoken" \
     -H "accept: application/json"

The API will return the attack’s details and let you know if the attack is in progress, halted, or successful.

 {
  "infra_target": {
    "resolvedHosts": [
      "172.31.13.93"
    ],
    "targetType": "Host",
    "strategy": {
      "multiSelectLabels": {},
      "multiSelectTags": {},
      "count": 1
    },
    "strategyType": "Random"
  },
  "target_type": "Host",
  "version": 4,
  "targets": [
    "172.31.13.93"
  ],
  "org_id": "c96634ae-1100-5b91-9e99-1e40bb2fe656",
  "args": [
    "cpu",
    "-c",
    "1",
    "--length",
    "30"
  ],
  "created_at": "2019-12-18T18:50:31.523Z",
  "create_source": "Api",
  "stage": "Successful",
  "execution_stage_summary": {
    "Successful": 1
  },
  "infra_command": {
    "commandType": "CPU",
    "commandArgs": {
      "allCores": false,
      "cores": 1,
      "length": 30,
      "percent": 100
    }
  },
  "stage_lifecycle": "Complete",
  "guid": "443bf563-21c7-11ea-b4dc-024221b6d1e6",
  "create_user": "anammedina21@gmail.com",
  "start_time": "2019-12-18T18:50:31.521Z",
  "end_time": "2019-12-18T18:51:19.539Z",
  "updated_at": "2019-12-18T18:51:19.540Z",
  "kind": "Api"
}

We can also verify the attack has run by going back to the Gremlin UI and navigating to our Attacks page.

When you select the specific attack you're running, you will be redirected to the attack details page that lets you see you've started this attack from the API

Conclusion

Congrats! You've setup Gremlin and learned to use the Gremlin API. To learn more about what you can do with our Gremlin API, checkout our API reference and API docs. If you have any questions at all or are wondering what else you can do with our API, feel free to DM me on the Chaos Slack: @anamedina (join here!). Good luck exploring!

Related

Avoid downtime. Use Gremlin to turn failure into resilience.

Gremlin empowers you to proactively root out failure before it causes downtime. See how you can harness chaos to build resilient systems by requesting a demo of Gremlin.

Request a Demo