Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.4 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  ListSecretsCommand,
} from '@aws-sdk/client-secrets-manager';
import { awsSecretsManagerCombinedAuth } from '../common/auth';
import { resolveSecretsManagerClient } from '../common/client';

export const findSecret = createAction({
  auth: awsSecretsManagerCombinedAuth,
  name: 'findSecret',
  classification: 'SEARCH',
  displayName: 'Find Secret',
  description: 'Finds an existing secret using filters.',
  audience: 'both',
  aiMetadata: {
    description:
      'Lists/searches secrets in AWS Secrets Manager, filtering by a chosen field (name, description, tag key, tag value, primary region, owning service, or "all") against a search value, with optional max results and sort. Use to discover secrets or resolve a name/ARN before another action. Read-only and idempotent.',
    idempotent: true,
  },
  props: {
    filterKey: Property.StaticDropdown({
      displayName: 'Filter Key',
      description: 'The field to filter by',
      required: true,
      options: {
        options: [
          { label: 'Name', value: 'name' },
          { label: 'Description', value: 'description' },
          { label: 'Tag Key', value: 'tag-key' },
          { label: 'Tag Value', value: 'tag-value' },
          { label: 'Primary Region', value: 'primary-region' },
          { label: 'Owning Service', value: 'owning-service' },
          { label: 'All', value: 'all' },
        ],
      },
    }),
    filterValue: Property.ShortText({
      displayName: 'Filter Value',
      description: 'The value to search for',
      required: true,
    }),
    maxResults: Property.Number({
      displayName: 'Max Results',
      description: 'Maximum number of results to return (1-100)',
      required: false,
    }),
    sortBy: Property.StaticDropdown({
      displayName: 'Sort By',
      description: 'Sort results by',
      required: false,
      options: {
        options: [
          { label: 'Created Date', value: 'created-date' },
          { label: 'Last Accessed Date', value: 'last-accessed-date' },
          { label: 'Last Changed Date', value: 'last-changed-date' },
          { label: 'Name', value: 'name' },
        ],
      },
    }),
    sortOrder: Property.StaticDropdown({
      displayName: 'Sort Order',
      description: 'Sort order for results',
      required: false,
      options: {
        options: [
          { label: 'Ascending', value: 'asc' },
          { label: 'Descending', value: 'desc' },
        ],
      },
    }),
  },
  async run({ auth, propsValue, server }) {
    const client = await resolveSecretsManagerClient({ auth: auth.props, server });

    try {
      const command = new ListSecretsCommand({
        Filters: [
          {
            Key: propsValue.filterKey as
              | 'description'
              | 'name'
              | 'tag-key'
              | 'tag-value'
              | 'primary-region'
              | 'owning-service'
              | 'all',
            Values: [propsValue.filterValue],
          },
        ],
        MaxResults: propsValue.maxResults,
        SortBy: propsValue.sortBy as any,
        SortOrder: propsValue.sortOrder as any,
      });

      const response = await client.send(command);

      return response;
    } catch (error: any) {
      throw new Error(
        `Failed to find secret: ${error.message ?? 'Unknown error'}`
      );
    }
  },
});