-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathcreate_connector_dir.sh
executable file
·137 lines (117 loc) · 3.58 KB
/
create_connector_dir.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
set -e
# Script to generate a new OpenCTI connector based on a template
# Usage: ./create_connector_dir.sh -t <TYPE> -n <NAME>
display_help() {
echo "Usage: $0 -t <TYPE> -n <NAME>" >&2
echo
echo " -t, --type Specify the connector type"
echo " -n, --name Specify the connector name"
echo " -h, --help Display this help message"
echo
echo "Example:"
echo " $0 -t stream -n qradar"
echo
exit 1
}
# Check for required options
if [ $# -eq 0 ]; then
display_help
exit 1
fi
# Parse options
while :
do
case "$1" in
-n | --name)
if [ -n "$2" ]; then
NAME="$2"
shift 2
else
echo "Error: '--name' requires a non-empty option argument." >&2
exit 1
fi
;;
-t | --type)
if [ -n "$2" ]; then
TYPE="$2"
shift 2
else
echo "Error: '--type' requires a non-empty option argument." >&2
exit 1
fi
;;
-h | --help)
display_help
;;
--)
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
display_help
exit 1
;;
*)
break
;;
esac
done
# Validate inputs
if [ -z "$TYPE" ] || [ -z "$NAME" ]; then
echo "Error: Both --type and --name are required arguments."
display_help
exit 1
fi
# Define accepted types
VALID_TYPES=("external-import" "internal-enrichment" "internal-export-file" "internal-import-file" "stream")
# Validate the connector type
TYPE_IS_VALID=false
for VALID_TYPE in "${VALID_TYPES[@]}"; do
if [ "$TYPE" == "$VALID_TYPE" ]; then
TYPE_IS_VALID=true
break
fi
done
if [ "$TYPE_IS_VALID" = false ]; then
echo "Error: Invalid connector type '$TYPE'!"
echo "Accepted types are: ${VALID_TYPES[*]}"
exit 1
fi
if [[ ! "$NAME" =~ ^[a-zA-Z0-9-]+$ ]]; then
echo "Error: Connector name '$NAME' is invalid!"
echo "The name can only contain letters, digits, and hyphens (-)."
exit 1
fi
# Enforce full lowercase name
NAME="${NAME,,}"
NEW_CONNECTOR_DIR="../$TYPE/$NAME"
TEMPLATE_DIR="$TYPE"
# Check if the template directory exists
if [ ! -d "$TEMPLATE_DIR" ]; then
echo "Error: Template directory '$TEMPLATE_DIR' does not exist. Execute this script in the template directory"
exit 1
fi
if [ -d "$NEW_CONNECTOR_DIR" ]; then
echo "Error: Connector directory '$NEW_CONNECTOR_DIR' already exists!"
exit 1
fi
# Create the new connector directory
echo "Creating new connector directory: $NEW_CONNECTOR_DIR"
mkdir -p "$NEW_CONNECTOR_DIR"
# Copy template files to the new directory
echo "Copying template files..."
cp -r "$TEMPLATE_DIR/"* "$NEW_CONNECTOR_DIR"
# Update placeholders in the copied files
echo "Customizing connector files..."
PYTHON_NAME="$(echo "$NAME" | sed -E 's/-([a-z])/\U\1/g' | sed -E 's/^(.)/\U\1/')"
CAPITALIZED_NAME=$(echo "$NAME" | sed 's/.*/\U&/' | sed -E 's/-/_/g')
find "$NEW_CONNECTOR_DIR" -type f -exec sed -i \
-e "s/template/$NAME/g" \
-e "s/ConnectorTemplate/Connector${PYTHON_NAME}/g" \
-e "s/TEMPLATE/${CAPITALIZED_NAME}/g" {} +
sed -i -e "s/$NAME/${NAME//-/_}/g" "$NEW_CONNECTOR_DIR/src/config.yml.sample"
sed -i -e "s/$NAME/${NAME//-/_}/g" "$NEW_CONNECTOR_DIR/src/${TYPE//-/_}_connector/config_loader.py"
echo "Connector '$NAME' of type '$TYPE' created successfully!"
echo "Navigate to $NEW_CONNECTOR_DIR to start development."