Skip to content

Instantly share code, notes, and snippets.

@bdpiprava
Last active March 26, 2018 07:02
Show Gist options
  • Save bdpiprava/db455c9b9c12c93a596bf85519a01c6d to your computer and use it in GitHub Desktop.
Save bdpiprava/db455c9b9c12c93a596bf85519a01c6d to your computer and use it in GitHub Desktop.
## Get plugin settings
- Plugin is not loaded
- Should return error response with failed dependecy status code(424)
- If the plugin is loaded then following checks we have to perform before returning response -
* If the plugin does not support plugin-settings then return an error message (have to decide what it will be :) )
* When plugin supports plugin setting check if plugin-setting exist for the plugin
a. If plugin setting does not exist then return 404.
b. When plugin settings exist in DB then
- Check if user is a admin
- encrypt the secure variables in plugin-settings using plugin info and return the response
## Create a plugin settings
- If Plugin is not loaded then return an error - failed dependency
- If Plugin is loaded then following checks we have to perform before returning response -
* If the plugin does not support plugin-settings then return an error message (have to decide what it will be :) )
* When plugin supports plugin setting check
a. If plugin settings already exists then return the error response
b. Validate with the plugin using extension
c. On successful validation store it in DB and return response else return 422 on validation failure
## Update a plugin settings
- If Plugin is not loaded then return an error
- If Plugin is loaded then following checks we have to perform before returning response -
* When plugin supports plugin setting check -
a. If plugin settings do not exist then return the error 404 response.
b. When plugin-settings exists to check if it is a stale request.
- Stale request,
* Return error response
- Not a stale request
* Validate with the plugin using extension
* On successful validation store it in DB and return response else return 422
In pluginDao saveOrUpdate should remove cache entry after the transaction is commited
@bdpiprava
Copy link
Author

bdpiprava commented Mar 20, 2018

Create plugin settings

Plugin is not loaded

  • Should return an error response with FAILED_DEPENDENCY(424) status code.

Plugin is loaded

Before storing plugin settings in DB it should perform following checks.

  • Check if a user is an admin, if a user is not an admin then return UNAUTHORIZED(401) status code.
  • Should return an error message with UNPROCESSABLE_ENTITY(422) status code when plugin id is not specified or it is empty.
  • The plugin does not support plugin-settings then return an error message with UNPROCESSABLE_ENTITY(422) status code.
    • I made an assumption here that if PluginInfo does not have any metadata about plugin-settings then it is not supported.

Take a lock on using keyToLockOn(pluginId) to block other create plugin settings requests. After taking lock run following validations

  • Should error out with UNPROCESSABLE_ENTITY(422) status code when plugin setting already created for the plugin.
  • Should validate plugin settings with the extensions and return UNPROCESSABLE_ENTITY(422) on validation error.

Update plugin settings

Plugin is not loaded

  • Should return an error response with FAILED_DEPENDENCY(424) status code.

Plugin is loaded

Before updating plugin settings in DB it should perform following checks.

  • Check if a user is an admin, if a user is not an admin then return UNAUTHORIZED(401) status code.
  • Should return an error message with UNPROCESSABLE_ENTITY(422) status code when plugin id is not specified or it is empty.
  • The plugin does not support plugin-settings then return an error message with UNPROCESSABLE_ENTITY(422) status code.
    • Same as create call

Take a lock on using keyToLockOn(pluginId) to block other create/update plugin settings requests. After taking lock run following validations

  • If plugin settings do not exist in DB then return NOT_FOUND(404) status code.
  • If plugin setting exists in DB then,
    • Should validate plugin settings with the extensions and return UNPROCESSABLE_ENTITY(422) on validation error.
  • Use an extension to validate the plugin settings
    • Successful Validation: Update the plugin settings in DB and remove the md5 from the cache using EntityHashingService
    • Validation Failure: return UNPROCESSABLE_ENTITY(422)

Get plugin settings

Plugin is not loaded

  • Should return an error response with FAILED_DEPENDENCY(424) status code.

Plugin is loaded

  • The plugin does not support plugin-settings then return an error message with UNPROCESSABLE_ENTITY(422) status code.
  • When plugin supports plugin settings then perform following checks:
    • Check if a user is an admin, if a user is not an admin then return UNAUTHORIZED(401) status code.
    • If plugin settings do not exist in DB then return NOT_FOUND(404) status code.
    • When plugin settings exist in DB then return it.
      • Make sure all the secure values are encrypted.

@bdpiprava
Copy link
Author

bdpiprava commented Mar 26, 2018

Get Plugin settings

When it does not exist.

Request

curl --request GET \
  --url 'http://{{GO_SERVER_URL}}/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm' \
  --header 'Accept: application/vnd.go.cd.v1+json'

Response status code: NOT_FOUND(404)

{
    "message": "Either the resource you requested was not found, or you are not authorized to perform this action."
}

When Plugin settings exist

Request

curl --request GET \
  --url 'http://{{GO_SERVER_URL}}/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm' \
  --header 'Accept: application/vnd.go.cd.v1+json'

Response status code: 200

{
    "_links": {
        "self": {
            "href": "http://localhost:8153/go/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm"
        },
        "doc": {
            "href": "https://api.gocd.org/#plugin-settings"
        },
        "find": {
            "href": "http://localhost:8153/go/api/admin/plugin_settings/:plugin_id"
        }
    },
    "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm",
    "configuration": [
        {
            "key": "environment_variables"
        },
        {
            "key": "docker_ca_cert"
        },
        {
            "key": "private_registry_username",
            "value": "docker-user"
        },
        {
            "key": "docker_uri",
            "value": "https://fmtgocddockermanager01.gocd.org:2376/"
        },
        {
            "key": "private_registry_server",
            "value": "https://docker.gocd.org"
        },
        {
            "key": "go_server_url",
            "value": "https://build.gocd.org:8154/go"
        },
        {
            "key": "docker_client_key"
        },
        {
            "key": "max_docker_containers",
            "value": "60"
        },
        {
            "key": "docker_client_cert"
        },
        {
            "key": "private_registry_password",
            "encrypted_value": "8xok/YGUAoU="
        },
        {
            "key": "enable_private_registry_authentication",
            "value": "true"
        },
        {
            "key": "auto_register_timeout",
            "value": "5"
        }
    ]
}

@bdpiprava
Copy link
Author

bdpiprava commented Mar 26, 2018

Create plugin settings when it is alreadu exist in DB

curl --request POST \
  --url 'http://{{GO_SERVER_URL}}/api/admin/plugin_settings' \
  --header 'Accept: application/vnd.go.cd.v1+json' \
  --header 'Content-Type: application/json' \
  --data '{
  "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm",
  "configuration": [
    {
      "key": "environment_variables"
    },
    {
      "key": "docker_ca_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_username",
      "value": "docker-user"
    },
    {
      "key": "docker_uri",
      "value": "https://fmtgocddockermanager01.gocd.org:2376/"
    },
    {
      "key": "private_registry_server",
      "value": "https://docker.gocd.org"
    },
    {
      "key": "go_server_url",
      "value": "https://build.gocd.org:8154/go"
    },
    {
      "key": "docker_client_key",
      "encrypted_value": ""
    },
    {
      "key": "max_docker_containers",
      "value": "60"
    },
    {
      "key": "docker_client_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_password",
      "value": ""
    },
    {
      "key": "enable_private_registry_authentication",
      "value": "true"
    },
    {
      "key": "auto_register_timeout",
      "value": "5"
    }
  ]
}
'

Response UNPROCESSABLE_ENTITY(422)

{
    "message": "Save failed. The plugin settings for plugin[cd.go.contrib.elastic-agent.docker-swarm] is already exist. In order to update the plugin settings refer the https://api.gocd.org/18.3.0/#update-plugin-settings.",
    "data": {
        "_links": {
            "self": {
                "href": "http://localhost:8153/go/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm"
            },
            "doc": {
                "href": "https://api.gocd.org/#plugin-settings"
            },
            "find": {
                "href": "http://localhost:8153/go/api/admin/plugin_settings/:plugin_id"
            }
        },
        "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm",
        "configuration": [
            {
                "key": "environment_variables"
            },
            {
                "key": "docker_ca_cert"
            },
            {
                "key": "private_registry_username",
                "value": "docker-user"
            },
            {
                "key": "docker_uri",
                "value": "https://fmtgocddockermanager01.gocd.org:2376/"
            },
            {
                "key": "private_registry_server",
                "value": "https://docker.gocd.org"
            },
            {
                "key": "go_server_url",
                "value": "https://build.gocd.org:8154/go"
            },
            {
                "key": "docker_client_key"
            },
            {
                "key": "max_docker_containers",
                "value": "60"
            },
            {
                "key": "docker_client_cert"
            },
            {
                "key": "private_registry_password"
            },
            {
                "key": "enable_private_registry_authentication",
                "value": "true"
            },
            {
                "key": "auto_register_timeout",
                "value": "5"
            }
        ]
    }
}

@bdpiprava
Copy link
Author

bdpiprava commented Mar 26, 2018

Update plugin settings -> 200

Request
curl --request PUT \
  --url 'http://{{GO_SERVER_URL}}/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm' \
  --header 'Accept: application/vnd.go.cd.v1+json' \
  --header 'Content-Type: application/json' \
  --header 'If-Match: "aa20416daae4f26191a211e2fda63d6d"' \
  --data '{
  "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm",
  "configuration": [
    {
      "key": "environment_variables"
    },
    {
      "key": "docker_ca_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_username",
      "value": "docker-user"
    },
    {
      "key": "docker_uri",
      "value": "https://fmtgocddockermanager01.gocd.org:2376/"
    },
    {
      "key": "private_registry_server",
      "value": "https://docker.gocd.org"
    },
    {
      "key": "go_server_url",
      "value": "https://build.gocd.org:8154/go"
    },
    {
      "key": "docker_client_key",
      "encrypted_value": ""
    },
    {
      "key": "max_docker_containers",
      "value": "60"
    },
    {
      "key": "docker_client_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_password",
      "value": "bar"
    },
    {
      "key": "enable_private_registry_authentication",
      "value": "true"
    },
    {
      "key": "auto_register_timeout",
      "value": "5"
    }
  ]
}
'
Response { "_links": { "self": { "href": "http://localhost:8153/go/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm" }, "doc": { "href": "https://api.gocd.org/#plugin-settings" }, "find": { "href": "http://localhost:8153/go/api/admin/plugin_settings/:plugin_id" } }, "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm", "configuration": [ { "key": "environment_variables" }, { "key": "docker_ca_cert" }, { "key": "private_registry_username", "value": "docker-user" }, { "key": "docker_uri", "value": "https://fmtgocddockermanager01.gocd.org:2376/" }, { "key": "private_registry_server", "value": "https://docker.gocd.org" }, { "key": "go_server_url", "value": "https://build.gocd.org:8154/go" }, { "key": "docker_client_key" }, { "key": "max_docker_containers", "value": "60" }, { "key": "docker_client_cert" }, { "key": "private_registry_password", "encrypted_value": "rw8RZMOKOqI=" }, { "key": "enable_private_registry_authentication", "value": "true" }, { "key": "auto_register_timeout", "value": "5" } ] }

@bdpiprava
Copy link
Author

Update plugin settings -> 422

Error on secure and plain text plugin settings porperties

Request
curl --request PUT \
  --url 'http://{{GO_SERVER_URL}}/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm' \
  --header 'Accept: application/vnd.go.cd.v1+json' \
  --header 'Content-Type: application/json' \
  --header 'If-Match: "aa20416daae4f26191a211e2fda63d6d"' \
  --data '{
  "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm",
  "configuration": [
    {
      "key": "environment_variables"
    },
    {
      "key": "docker_ca_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_username",
      "value": "docker-user"
    },
    {
      "key": "docker_uri",
      "value": ""
    },
    {
      "key": "private_registry_server",
      "value": "https://docker.gocd.org"
    },
    {
      "key": "go_server_url",
      "value": "https://build.gocd.org:8154/go"
    },
    {
      "key": "docker_client_key",
      "encrypted_value": ""
    },
    {
      "key": "max_docker_containers",
      "value": "60"
    },
    {
      "key": "docker_client_cert",
      "encrypted_value": ""
    },
    {
      "key": "private_registry_password",
      "value": ""
    },
    {
      "key": "enable_private_registry_authentication",
      "value": "true"
    },
    {
      "key": "auto_register_timeout",
      "value": "5"
    }
  ]
}
'
Response { "message": "Save failed. There are errors in the plugin settings. Please fix them and resubmit.", "data": { "_links": { "self": { "href": "http://localhost:8153/go/api/admin/plugin_settings/cd.go.contrib.elastic-agent.docker-swarm" }, "doc": { "href": "https://api.gocd.org/#plugin-settings" }, "find": { "href": "http://localhost:8153/go/api/admin/plugin_settings/:plugin_id" } }, "plugin_id": "cd.go.contrib.elastic-agent.docker-swarm", "configuration": [ { "key": "environment_variables" }, { "key": "docker_ca_cert" }, { "key": "private_registry_username", "value": "docker-user" }, { "errors": { "docker_uri": [ "Docker URI must not be blank." ] }, "key": "docker_uri", "value": "" }, { "key": "private_registry_server", "value": "https://docker.gocd.org" }, { "key": "go_server_url", "value": "https://build.gocd.org:8154/go" }, { "key": "docker_client_key" }, { "key": "max_docker_containers", "value": "60" }, { "key": "docker_client_cert" }, { "errors": { "private_registry_password": [ "Private Registry Password must not be blank." ] }, "key": "private_registry_password" }, { "key": "enable_private_registry_authentication", "value": "true" }, { "key": "auto_register_timeout", "value": "5" } ] } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment