|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +read -p "Enter the new investigation (package) name: " INVESTIGATION_NAME |
| 6 | +if [[ "${INVESTIGATION_NAME}" == "" ]] ; then |
| 7 | + echo "Investigation name cannot be empty." |
| 8 | + exit 1 |
| 9 | +elif [[ "${INVESTIGATION_NAME}" =~ [^a-zA-Z0-9_] ]] ; then |
| 10 | + echo "Investigation name must be alphanumeric." |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +read -p "Enter new investigation description: " INVESTIGATION_DESCRIPTION |
| 15 | +if [[ "${INVESTIGATION_DESCRIPTION}" == "" ]] ; then |
| 16 | + INVESTIGATION_DESCRIPTION="TODO" |
| 17 | +fi |
| 18 | + |
| 19 | +read -p "Should Investigate Alert (y/n): " INVESTIGATE_ALERT_BOOL |
| 20 | +if [[ "${INVESTIGATE_ALERT_BOOL}" == "y" ]] ; then |
| 21 | + read -p "Investigation alert string: " INVESTIGATION_ALERT_STRING |
| 22 | + INVESTIGATION_ALERT="strings.Contains(alert, \"${INVESTIGATION_ALERT_STRING}\")" |
| 23 | +elif [[ "${INVESTIGATE_ALERT_BOOL}" == "n" ]] ; then |
| 24 | + INVESTIGATION_ALERT="false" |
| 25 | +else |
| 26 | + echo "Invalid input. Please enter 'y' or 'n'." |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +INVESTIGATION_NAME=$(echo "${INVESTIGATION_NAME}" | tr '[:upper:]' '[:lower:]') |
| 31 | + |
| 32 | +INVESTIGATION_DIR="../pkg/investigations/${INVESTIGATION_NAME}" |
| 33 | + |
| 34 | +if [ -d "${INVESTIGATION_DIR}" ]; then |
| 35 | + echo "Investigation of name ${INVESTIGATION_NAME} already exists." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +mkdir -p "${INVESTIGATION_DIR}" |
| 40 | +ls "${INVESTIGATION_DIR}" |
| 41 | + |
| 42 | +touch "${INVESTIGATION_DIR}/${INVESTIGATION_NAME}.go" |
| 43 | +touch "${INVESTIGATION_DIR}/metadata.yaml" |
| 44 | +touch "${INVESTIGATION_DIR}/README.md" |
| 45 | + |
| 46 | +# Create README.md file |
| 47 | +cat <<EOF > "${INVESTIGATION_DIR}/README.md" |
| 48 | +# ${INVESTIGATION_NAME} Investigation |
| 49 | +
|
| 50 | +${INVESTIGATION_DESCRIPTION} |
| 51 | +
|
| 52 | +EOF |
| 53 | + |
| 54 | +# Create metadata.yaml file |
| 55 | +cat <<EOF > "${INVESTIGATION_DIR}/metadata.yaml" |
| 56 | +name: ${INVESTIGATION_NAME} |
| 57 | +rbac: |
| 58 | + roles: [] |
| 59 | + clusterRoleRules: [] |
| 60 | +customerDataAccess: false |
| 61 | +
|
| 62 | +EOF |
| 63 | + |
| 64 | +# Create boilerplate investigation file |
| 65 | +cat <<EOF > "${INVESTIGATION_DIR}/${INVESTIGATION_NAME}.go" |
| 66 | +// Package ${INVESTIGATION_NAME} contains...TODO |
| 67 | +package ${INVESTIGATION_NAME} |
| 68 | +
|
| 69 | +import ( |
| 70 | + "strings" |
| 71 | +
|
| 72 | + "github.com/openshift/configuration-anomaly-detection/pkg/investigations/investigation" |
| 73 | + "github.com/openshift/configuration-anomaly-detection/pkg/logging" |
| 74 | + "github.com/openshift/configuration-anomaly-detection/pkg/notewriter" |
| 75 | +) |
| 76 | +
|
| 77 | +type Investigation struct{} |
| 78 | +
|
| 79 | +func (c *Investigation) Run(r *investigation.Resources) (investigation.InvestigationResult, error) { |
| 80 | + result := investigation.InvestigationResult{} |
| 81 | +
|
| 82 | + // Initialize PagerDuty note writer |
| 83 | + notes := notewriter.New(r.Name, logging.RawLogger) |
| 84 | +
|
| 85 | + // TODO: Implement investigation logic here |
| 86 | +
|
| 87 | + return result, r.PdClient.EscalateIncidentWithNote(notes.String()) |
| 88 | +} |
| 89 | +
|
| 90 | +func (c *Investigation) Name() string { |
| 91 | + return "${INVESTIGATION_NAME}" |
| 92 | +} |
| 93 | +
|
| 94 | +func (c *Investigation) Description() string { |
| 95 | + return "${INVESTIGATION_DESCRIPTION}" |
| 96 | +} |
| 97 | +
|
| 98 | +func (c *Investigation) ShouldInvestigateAlert(alert string) bool { |
| 99 | + return ${INVESTIGATION_ALERT} |
| 100 | +} |
| 101 | +
|
| 102 | +func (c *Investigation) IsExperimental() bool { |
| 103 | + // TODO: Update to false when graduating to production. |
| 104 | + return true |
| 105 | +} |
| 106 | +
|
| 107 | +EOF |
| 108 | + |
| 109 | +echo "${INVESTIGATION_NAME} created in ${INVESTIGATION_DIR}" |
| 110 | +echo "metadata.yaml file created in ${INVESTIGATION_DIR}" |
| 111 | + |
| 112 | +# Update registry.go to contain new investigation |
| 113 | +if ! grep -q "${INVESTIGATION_NAME}" ../pkg/investigations/registry.go && ! grep -q "${INVESTIGATION_NAME}" ../pkg/investigations/registry.go; then |
| 114 | + sed -i "/import (/a \\\t\"github.com/openshift/configuration-anomaly-detection/pkg/investigations/${INVESTIGATION_NAME}\"" ../pkg/investigations/registry.go |
| 115 | + sed -i "/var availableInvestigations = \[/a \\\t&${INVESTIGATION_NAME}.Investigation{}," ../pkg/investigations/registry.go |
| 116 | + echo "${INVESTIGATION_NAME} added to registry.go" |
| 117 | +else |
| 118 | + echo "${INVESTIGATION_NAME} already exists in registry.go" |
| 119 | +fi |
0 commit comments