|
| 1 | +Plugins |
| 2 | +======= |
| 3 | +The |cdk| toolkit provides extension points that enable users to augment the features provided by |
| 4 | +the toolkit. There is currently only one extension point, allowing users to define custom AWS |
| 5 | +credential providers, but other extension points may be added in the future as the needs arise. |
| 6 | +Loading Plugins |
| 7 | +--------------- |
| 8 | +Plugins can be loaded by providing the Node module name (or path) to the CDK toolkit: |
| 9 | +1. Using the ``--plugin`` command line option (which can be specified multiple times): |
| 10 | + .. code-block:: console |
| 11 | + $ cdk list --plugin=module |
| 12 | + $ cdk deploy --plugin=module_1 --plugin=module_2 |
| 13 | +2. Adding entries to the ``~/.cdk.json`` or ``cdk.json`` file: |
| 14 | + .. code-block:: js |
| 15 | + { |
| 16 | + // ... |
| 17 | + "plugin": [ |
| 18 | + "module_1", |
| 19 | + "module_2" |
| 20 | + ], |
| 21 | + // ... |
| 22 | + } |
| 23 | +Authoring Plugins |
| 24 | +----------------- |
| 25 | +Plugins must be authored in TypeScript or Javascript, and are defined by implementing a Node module |
| 26 | +that implements the following protocol, and using :js:class:`~aws-cdk.PluginHost` methods: |
| 27 | +.. tabs:: |
| 28 | + .. code-tab:: typescript |
| 29 | + import cdk = require('aws-cdk'); |
| 30 | + export = { |
| 31 | + version: '1', // Version of the plugin infrastructure (currently always '1') |
| 32 | + init(host: cdk.PluginHost): void { |
| 33 | + // Your plugin initialization hook. |
| 34 | + // Use methods of ``host`` to register custom code with the CDK toolkit |
| 35 | + } |
| 36 | + }; |
| 37 | + .. code-tab:: javascript |
| 38 | + export = { |
| 39 | + version: '1', // Version of the plugin infrastructure (currently always '1') |
| 40 | + init(host) { |
| 41 | + // Your plugin initialization hook. |
| 42 | + // Use methods of ``host`` to register custom code with the CDK toolkit |
| 43 | + } |
| 44 | + }; |
| 45 | +Credential Provider Plugins |
| 46 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 47 | +Custom credential providers are classes implementing the |
| 48 | +:js:class:`~aws-cdk.CredentialProviderSource` interface, and registered to the toolkit using |
| 49 | +the :js:func:`~aws-cdk.PluginHost.registerCredentialProviderSource` method. |
| 50 | +.. tabs:: |
| 51 | + .. code-tab:: typescript |
| 52 | + import cdk = require('aws-cdk'); |
| 53 | + import aws = require('aws-sdk'); |
| 54 | + class CustomCredentialProviderSource implements cdk.CredentialProviderSource { |
| 55 | + public async isAvailable(): Promise<boolean> { |
| 56 | + // Return ``false`` if the plugin could determine it cannot be used (for example, |
| 57 | + // if it depends on files that are not present on this host). |
| 58 | + return true; |
| 59 | + } |
| 60 | + public async canProvideCredentials(accountId: string): Promise<boolean> { |
| 61 | + // Return ``false`` if the plugin is unable to provide credentials for the |
| 62 | + // requested account (for example if it's not managed by the credentials |
| 63 | + // system that this plugin adds support for). |
| 64 | + return true; |
| 65 | + } |
| 66 | + public async getProvider(accountId: string, mode: cdk.Mode): Promise<aws.Credentials> { |
| 67 | + let credentials: aws.Credentials; |
| 68 | + // Somehow obtain credentials in ``credentials``, and return those. |
| 69 | + return credentials; |
| 70 | + } |
| 71 | + } |
| 72 | + export = { |
| 73 | + version = '1', |
| 74 | + init(host: cdk.PluginHost): void { |
| 75 | + // Register the credential provider to the PluginHost. |
| 76 | + host.registerCredentialProviderSource(new CustomCredentialProviderSource()); |
| 77 | + } |
| 78 | + }; |
| 79 | + .. code-tab:: javascript |
| 80 | + class CustomCredentialProviderSource { |
| 81 | + async isAvailable() { |
| 82 | + // Return ``false`` if the plugin could determine it cannot be used (for example, |
| 83 | + // if it depends on files that are not present on this host). |
| 84 | + return true; |
| 85 | + } |
| 86 | + async canProvideCredentials(accountId) { |
| 87 | + // Return ``false`` if the plugin is unable to provide credentials for the |
| 88 | + // requested account (for example if it's not managed by the credentials |
| 89 | + // system that this plugin adds support for). |
| 90 | + return true; |
| 91 | + } |
| 92 | + async getProvider(accountId, mode) { |
| 93 | + let credentials; |
| 94 | + // Somehow obtain credentials in ``credentials``, and return those. |
| 95 | + return credentials; |
| 96 | + } |
| 97 | + } |
| 98 | + export = { |
| 99 | + version = '1', |
| 100 | + init(host) { |
| 101 | + // Register the credential provider to the PluginHost. |
| 102 | + host.registerCredentialProviderSource(new CustomCredentialProviderSource()); |
| 103 | + } |
| 104 | + }; |
| 105 | +Note that the credentials obtained from the providers for a given account and mode will be cached, |
| 106 | +and as a consequence it is strongly advised that the credentials objects returned are self-refreshing, |
| 107 | +as descibed in the `AWS SDK for Javascript documentation <https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html>`_. |
| 108 | +Reference |
| 109 | +--------- |
| 110 | +.. js:module:: aws-cdk |
| 111 | +CredentialProviderSource *(interface)* |
| 112 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 113 | +.. js:class:: CredentialProviderSource |
| 114 | + .. js:attribute:: name |
| 115 | + A friendly name for the provider, which will be used in error messages, for example. |
| 116 | + :type: ``string`` |
| 117 | + .. js:method:: isAvailable() |
| 118 | + Whether the credential provider is even online. Guaranteed to be called before any of the other functions are called. |
| 119 | + :returns: ``Promise<boolean>`` |
| 120 | + .. js:method:: canProvideCredentials(accountId) |
| 121 | + Whether the credential provider can provide credentials for the given account. |
| 122 | + :param string accountId: the account ID for which credentials are needed. |
| 123 | + :returns: ``Promise<boolean>`` |
| 124 | + .. js:method:: getProvider(accountId, mode) |
| 125 | + Construct a credential provider for the given account and the given access mode. |
| 126 | + Guaranteed to be called only if canProvideCredentails() returned true at some point. |
| 127 | + :param string accountId: the account ID for which credentials are needed. |
| 128 | + :param aws-cdk.Mode mode: the kind of operations the credentials are intended to perform. |
| 129 | + :returns: ``Promise<aws.Credentials>`` |
| 130 | +Mode *(enum)* |
| 131 | +^^^^^^^^^^^^^ |
| 132 | +.. js:class:: Mode |
| 133 | + .. js:data:: ForReading |
| 134 | + Credentials are inteded to be used for read-only scenarios. |
| 135 | + .. js:data:: ForWriting |
| 136 | + Credentials are intended to be used for read-write scenarios. |
| 137 | +Plugin *(interface)* |
| 138 | +^^^^^^^^^^^^^^^^^^^^ |
| 139 | +.. js:class:: Plugin() |
| 140 | + .. js:attribute:: version |
| 141 | + The version of the plug-in interface used by the plug-in. This will be used by |
| 142 | + the plug-in host to handle version changes. Currently, the only valid value for |
| 143 | + this attribute is ``'1'``. |
| 144 | + :type: ``string`` |
| 145 | + .. js:method:: init(host) |
| 146 | + When defined, this function is invoked right after the plug-in has been loaded, |
| 147 | + so that the plug-in is able to initialize itself. It may call methods of the |
| 148 | + :js:class:`~aws-cdk.PluginHost` instance it receives to register new |
| 149 | + :js:class:`~aws-cdk.CredentialProviderSource` instances. |
| 150 | + :param aws-cdk.PluginHost host: The |cdk| plugin host |
| 151 | + :returns: ``void`` |
| 152 | +PluginHost |
| 153 | +^^^^^^^^^^ |
| 154 | +.. js:class:: PluginHost() |
| 155 | + .. js:data:: instance |
| 156 | + :type: :js:class:`~aws-cdk.PluginHost` |
| 157 | + .. js:method:: load(moduleSpec) |
| 158 | + Loads a plug-in into this PluginHost. |
| 159 | + :param string moduleSpec: the specification (path or name) of the plug-in module to be loaded. |
| 160 | + :throws Error: if the provided ``moduleSpec`` cannot be loaded or is not a valid :js:class:`~aws-cdk.Plugin`. |
| 161 | + :returns: ``void`` |
| 162 | + .. js:method:: registerCredentialProviderSource(source) |
| 163 | + Allows plug-ins to register new :js:class:`~aws-cdk.CredentialProviderSources`. |
| 164 | + :param aws-cdk.CredentialProviderSources source: a new CredentialProviderSource to register. |
| 165 | + :returns: ``void`` |
0 commit comments