Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, props, reducers } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'

import { IntegrationType } from '~/types'

export type S3AuthMode = 'role' | 'access_key'

const IAM_ROLE_ARN_REGEX = /^arn:aws:iam::\d{12}:role\/.+$/

export interface AwsS3SetupModalLogicProps {
    isOpen: boolean
    integration?: IntegrationType | null
    onComplete: (integrationId?: number) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface awsS3SetupModalLogicValues {
    authMode: S3AuthMode
    awsS3Integration: {
        awsAccessKeyId: string
        awsRoleArn: string
        awsSecretAccessKey: string
        name: string
    }
    awsS3IntegrationAllErrors: Record<string, any>
    awsS3IntegrationChanged: boolean
    awsS3IntegrationErrors: DeepPartialMap<
        {
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        },
        ValidationErrorType
    >
    awsS3IntegrationHasErrors: boolean
    awsS3IntegrationManualErrors: Record<string, any>
    awsS3IntegrationTouched: boolean
    awsS3IntegrationTouches: Record<string, boolean>
    awsS3IntegrationValidationErrors: DeepPartialMap<
        {
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        },
        ValidationErrorType
    >
    isAwsS3IntegrationSubmitting: boolean
    isAwsS3IntegrationValid: boolean
    showAwsS3IntegrationErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface awsS3SetupModalLogicActions {
    loadIntegrations: (_?: any) => any // integrationsLogic
    resetAwsS3Integration: (values?: {
        awsAccessKeyId: string
        awsRoleArn: string
        awsSecretAccessKey: string
        name: string
    }) => {
        values?: {
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        }
    }
    setAuthMode: (mode: S3AuthMode) => {
        mode: S3AuthMode
    }
    setAwsS3IntegrationManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setAwsS3IntegrationValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setAwsS3IntegrationValues: (
        values: DeepPartial<{
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        }>
    ) => {
        values: DeepPartial<{
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        }>
    }
    submitAwsS3Integration: () => {
        value: boolean
    }
    submitAwsS3IntegrationFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitAwsS3IntegrationRequest: (awsS3Integration: {
        awsAccessKeyId: string
        awsRoleArn: string
        awsSecretAccessKey: string
        name: string
    }) => {
        awsS3Integration: {
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        }
    }
    submitAwsS3IntegrationSuccess: (awsS3Integration: {
        awsAccessKeyId: string
        awsRoleArn: string
        awsSecretAccessKey: string
        name: string
    }) => {
        awsS3Integration: {
            awsAccessKeyId: string
            awsRoleArn: string
            awsSecretAccessKey: string
            name: string
        }
    }
    touchAwsS3IntegrationField: (key: string) => {
        key: string
    }
}

export type awsS3SetupModalLogicType = MakeLogicType<
    awsS3SetupModalLogicValues,
    awsS3SetupModalLogicActions,
    AwsS3SetupModalLogicProps
>

export const awsS3SetupModalLogic = kea<awsS3SetupModalLogicType>([
    path(['integrations', 'aws-s3', 'awsS3SetupModalLogic']),
    props({} as AwsS3SetupModalLogicProps),
    connect(() => ({
        actions: [integrationsLogic, ['loadIntegrations']],
    })),
    actions({
        setAuthMode: (mode: S3AuthMode) => ({ mode }),
    }),
    reducers({
        authMode: [
            'role' as S3AuthMode,
            {
                setAuthMode: (_, { mode }) => mode,
            },
        ],
    }),
    forms(({ props, actions, values }) => ({
        awsS3Integration: {
            defaults: {
                name: '',
                awsAccessKeyId: '',
                awsSecretAccessKey: '',
                awsRoleArn: '',
            },
            errors: ({ name, awsAccessKeyId, awsSecretAccessKey, awsRoleArn }) => ({
                name: name.trim() ? undefined : 'Name is required',
                awsAccessKeyId:
                    values.authMode === 'role' || awsAccessKeyId.trim() ? undefined : 'Access Key ID is required',
                awsSecretAccessKey:
                    values.authMode === 'role' || awsSecretAccessKey.trim()
                        ? undefined
                        : 'Secret Access Key is required',
                awsRoleArn:
                    values.authMode !== 'role'
                        ? undefined
                        : !awsRoleArn.trim()
                          ? 'IAM role ARN is required'
                          : !IAM_ROLE_ARN_REGEX.test(awsRoleArn.trim())
                            ? 'Enter a valid IAM role ARN, e.g. arn:aws:iam::123456789012:role/my-role'
                            : undefined,
            }),
            submit: async () => {
                try {
                    const { name, awsAccessKeyId, awsSecretAccessKey, awsRoleArn } = values.awsS3Integration
                    const integration = await api.integrations.create({
                        kind: 'aws-s3',
                        config:
                            values.authMode === 'role'
                                ? {
                                      name,
                                      aws_role_arn: awsRoleArn.trim(),
                                  }
                                : {
                                      name,
                                      aws_access_key_id: awsAccessKeyId,
                                      aws_secret_access_key: awsSecretAccessKey,
                                  },
                    })
                    actions.loadIntegrations()
                    lemonToast.success('AWS S3 connection created successfully!')
                    props.onComplete(integration.id)
                } catch (error: any) {
                    lemonToast.error(error.detail || 'Failed to create AWS S3 connection')
                    throw error
                }
            },
        },
    })),
    listeners(({ actions }) => ({
        setAuthMode: () => {
            actions.setAwsS3IntegrationValues({
                awsAccessKeyId: '',
                awsSecretAccessKey: '',
                awsRoleArn: '',
            })
        },
    })),
])