/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useState, useEffect } from "react"; import HeaderPanel from "components/HeaderPanel"; import PagePanel from "components/PagePanel"; import Tiles from "components/Tiles"; import { CreateLogMethod } from "assets/js/const"; import FormItem from "components/FormItem"; import { SelectItem } from "components/Select/select"; import { appSyncRequestQuery } from "assets/js/request"; import { LoggingBucket, LoggingBucketSource, Resource, ResourceType, } from "API"; import { getResourceLoggingBucket, listResources } from "graphql/queries"; import AutoComplete from "components/AutoComplete"; import { WAFTaskProps } from "../CreateWAF"; import { OptionType } from "components/AutoComplete/autoComplete"; import TextInput from "components/TextInput"; import { AppStateProps, InfoBarTypes } from "reducer/appReducer"; import { useTranslation } from "react-i18next"; import AutoEnableLogging from "../../common/AutoEnableLogging"; import { buildWAFLink } from "assets/js/utils"; import { AmplifyConfigType } from "types"; import { useSelector } from "react-redux"; import CrossAccountSelect from "pages/comps/account/CrossAccountSelect"; import IngestOptionSelect, { IngestOption } from "./IngestOptionSelect"; import SampleSchedule from "./SampleSchedule"; export enum WAF_TYPE { CLOUDFRONT = "CLOUDFRONT", REGIONAL = "REGIONAL", } interface SpecifySettingsProps { wafTask: WAFTaskProps; changeTaskType: (type: string) => void; changeWAFBucket: (bucket: string) => void; changeWAFObj: (waf: OptionType | null) => void; changeLogPath: (logPath: string) => void; manualChangeACL: (acl: string) => void; autoWAFEmptyError: boolean; manualAclEmptyError: boolean; manualWAFEmptyError: boolean; manualS3PathInvalid: boolean; intervalValueError: boolean; setNextStepDisableStatus: (status: boolean) => void; setISChanging: (changing: boolean) => void; changeNeedEnableLogging: (need: boolean) => void; changeCrossAccount: (id: string) => void; changeIngestionOption: (option: string) => void; changeScheduleInterval: (interval: string) => void; changeLogSource: (source: string) => void; } const SpecifySettings: React.FC = ( props: SpecifySettingsProps ) => { const { wafTask, changeWAFObj, manualChangeACL, changeWAFBucket, changeTaskType, changeLogPath, autoWAFEmptyError, manualAclEmptyError, manualWAFEmptyError, manualS3PathInvalid, intervalValueError, setNextStepDisableStatus, changeNeedEnableLogging, setISChanging, changeCrossAccount, changeIngestionOption, changeScheduleInterval, changeLogSource, } = props; const { t } = useTranslation(); const [loadingWAFList, setLoadingWAFList] = useState(false); const [loadingBucket, setLoadingBucket] = useState(false); const [wafOptionList, setWAFOptionList] = useState([]); const [showInfoText, setShowInfoText] = useState(false); const [showSuccessText, setShowSuccessText] = useState(false); const [previewS3Path, setPreviewS3Path] = useState(""); const [disableWAF, setDisableWAF] = useState(false); const amplifyConfig: AmplifyConfigType = useSelector( (state: AppStateProps) => state.amplifyConfig ); const getWAFList = async (accountId: string) => { try { setLoadingWAFList(true); const resData: any = await appSyncRequestQuery(listResources, { type: ResourceType.WAF, accountId: accountId, }); console.info("domainNames:", resData.data); const dataList: Resource[] = resData.data.listResources; const tmpOptionList: SelectItem[] = []; dataList.forEach((element) => { tmpOptionList.push({ name: `${element.name}`, value: element.id, description: element.description || "", }); }); setWAFOptionList(tmpOptionList); setLoadingWAFList(false); } catch (error) { console.error(error); } }; const getBucketPrefix = async (bucket: string) => { setLoadingBucket(true); setISChanging(true); const resData: any = await appSyncRequestQuery(getResourceLoggingBucket, { type: ResourceType.WAF, resourceName: bucket, accountId: wafTask.logSourceAccountId, }); console.info("getBucketPrefix:", resData.data); const logginBucket: LoggingBucket = resData?.data?.getResourceLoggingBucket; setLoadingBucket(false); setISChanging(false); setPreviewS3Path(` s3://${logginBucket.bucket}/${logginBucket.prefix}`); if (logginBucket.enabled) { changeWAFBucket(logginBucket.bucket || ""); changeLogPath(logginBucket.prefix || ""); changeLogSource(logginBucket.source || ""); setShowSuccessText(true); } else { setShowInfoText(true); setShowSuccessText(false); } }; useEffect(() => { setShowSuccessText(false); setShowInfoText(false); setNextStepDisableStatus(false); if ( wafTask.params.wafObj && wafTask.params.wafObj.value && wafTask.params.ingestOption === IngestOption.FullRequest ) { getBucketPrefix(wafTask.params.wafObj.value); } }, [wafTask.params.wafObj, wafTask.params.ingestOption]); useEffect(() => { if (wafTask.params.taskType === CreateLogMethod.Automatic) { getWAFList(wafTask.logSourceAccountId); } }, [wafTask.logSourceAccountId, wafTask.params.taskType]); useEffect(() => { changeNeedEnableLogging(showInfoText); }, [showInfoText]); return (
{ changeTaskType(event.target.value); changeWAFObj(null); if (event.target.value === CreateLogMethod.Automatic) { changeLogPath(""); } }} items={[ { label: t("servicelog:waf.auto"), description: t("servicelog:waf.autoDesc"), value: CreateLogMethod.Automatic, }, { label: t("servicelog:waf.manual"), description: t("servicelog:waf.manualDesc"), value: CreateLogMethod.Manual, }, ]} />
{ changeCrossAccount(id); changeWAFObj(null); }} loadingAccount={(loading) => { setDisableWAF(loading); }} /> {wafTask.params.taskType === CreateLogMethod.Automatic && (
{t("servicelog:waf.aclDesc")}
} errorText={ autoWAFEmptyError ? t("servicelog:waf.aclEmptyError") : "" } > , data ) => { changeWAFObj(data); }} /> { changeIngestionOption(option); }} warningText={ showSuccessText && wafTask.params.logSource === LoggingBucketSource.WAF ? (
{t("servicelog:waf.sourceWAFTip")}
) : ( "" ) } successText={ showSuccessText && previewS3Path && wafTask.params.logSource === LoggingBucketSource.KinesisDataFirehoseForWAF ? t("servicelog:waf.savedTips") + previewS3Path : "" } /> {showInfoText && ( { setISChanging(status); }} changeLogSource={(source) => { changeLogSource(source); }} changeLogBucketAndPrefix={(bucket, prefix, enabled) => { changeWAFBucket(bucket || ""); changeLogPath(prefix || ""); if (enabled) { setPreviewS3Path(` s3://${bucket}/${prefix}`); setShowSuccessText(true); setShowInfoText(false); } else { setShowInfoText(true); setShowSuccessText(false); } }} /> )} {wafTask.params.ingestOption === IngestOption.SampledRequest && ( { changeScheduleInterval(interval); }} errorText={ intervalValueError ? t("servicelog:waf.sampleScheduleError") : "" } /> )}
)} {wafTask.params.taskType === CreateLogMethod.Manual && (
{ manualChangeACL(event.target.value); }} /> { changeIngestionOption(option); }} /> {wafTask.params.ingestOption === IngestOption.FullRequest && ( { changeLogPath(event.target.value); }} /> )} {wafTask.params.ingestOption === IngestOption.SampledRequest && ( { changeScheduleInterval(interval); }} errorText={ intervalValueError ? t("servicelog:waf.sampleScheduleError") : "" } /> )}
)}
); }; export default SpecifySettings;