Matches source and destination addresses with IP regex, and uses the appropriate selector in the YAML output

This commit is contained in:
2023-01-31 15:10:51 -05:00
parent 6673b72fbd
commit c3e4c03dec
3 changed files with 113 additions and 45 deletions

View File

@@ -3,7 +3,7 @@ OUTPUT=mkconfig
build: $(SOURCE_FILES)
mkdir -p bin/ && \
gcc -Wall $(CFLAGS) $(LIBS) $(SOURCE_FILES) -o bin/$(OUTPUT)
gcc $(CFLAGS) $(LIBS) $(SOURCE_FILES) -o bin/$(OUTPUT)
run:
bin/$(OUTPUT)

16
README.md Normal file
View File

@@ -0,0 +1,16 @@
# Running the code
1. Clone the repo
```
git clone https://git.jagrajaulakh.com/juggy1233/COMP4990-task7.git task7
cd task7
```
2. Compile code and run
```
make build
bin/mkconfig nginx 80 access 5978 TCP -o test.yaml
```

View File

@@ -1,9 +1,96 @@
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
enum { SELECTOR_TYPE_IP, SELECTOR_TYPE_POD_NAME };
// Assigns a type depending if addr matchs IP regex
void findAddressType(char *addr, int *type) {
regex_t ip_addr_regex;
int result = regcomp(
&ip_addr_regex, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(/[0-9][0-9]?)?$",
REG_EXTENDED);
if (result == 1) { // REGEX FAILED TO COMPILE
return;
}
int match = !regexec(&ip_addr_regex, addr, 0, NULL, 0);
if (match) { // NO MATCH
*type = SELECTOR_TYPE_IP;
return;
}
*type = SELECTOR_TYPE_POD_NAME;
}
void checkArgs(char *sourceaddr, int *sourcetype, char *destaddr,
int *desttype) {
findAddressType(sourceaddr, sourcetype);
findAddressType(destaddr, desttype);
}
void writeFile(char *outputfilename, char *sourceaddr, int sourcetype,
int sourceport, char *destaddr, int desttype, int destport,
char *protocol) {
const char *yaml_template =
"apiVersion: networking.k8s.io/v1\n"
"kind: NetworkPolicy\n"
"metadata:\n"
" name: test-network-policy\n"
" namespace: policy-demo\n"
"spec:\n"
" podSelector:\n"
" matchLabels:\n"
" run: nginx\n"
" ingress:\n"
" - from:\n%s"
" ports:\n"
" - protocol: %s\n"
" port: %d\n"
" egress:\n"
" - to:\n%s"
" ports:\n"
" - protocol: %s\n"
" port: %d\n";
const char *ip_template =
" - ipBlock:\n"
" cidr: %s\n";
const char *pod_template =
" - podSelector:\n"
" matchLabels:\n"
" run: %s\n";
const char *ingress_template =
sourcetype == SELECTOR_TYPE_IP ? ip_template : pod_template;
const char *egress_template =
desttype == SELECTOR_TYPE_IP ? ip_template : pod_template;
char outputBuf[2000], ingress_template_filled[1000],
egress_template_filled[1000];
sprintf(ingress_template_filled, ingress_template, sourceaddr);
sprintf(egress_template_filled, egress_template, destaddr);
sprintf(outputBuf, yaml_template, ingress_template_filled, protocol,
sourceport, egress_template_filled, protocol, destport);
// puts("----------------------");
// printf("%s\n", ingress_template_filled);
// puts("----------------------");
// printf("%s\n", egress_template_filled);
// puts("----------------------");
// printf("%s\n", outputBuf);
// puts("----------------------");
FILE *outputFile = fopen(outputfilename, "w");
fputs(outputBuf, outputFile);
fclose(outputFile);
}
int main(int argc, char *argv[]) {
if (argc < 6) {
fprintf(stderr,
@@ -44,53 +131,18 @@ int main(int argc, char *argv[]) {
sscanf(argv[optind + 3], "%d", &destport);
char *protocol = argv[optind + 4];
// printf(
// "Source address: %s\nSource port: %d\nDestination address: "
// "%s\nDestination port: %d\nProtocol: %s\nOutput: %s\n",
// sourceaddr, sourceport, destaddr, destport, protocol, output);
int sourcetype, desttype;
const char *template =
"apiVersion: networking.k8s.io/v1\n"
"kind: NetworkPolicy\n"
"metadata:\n"
" name: test-network-policy\n"
" namespace: policy-demo\n"
"spec:\n"
" podSelector:\n"
" matchLabels:\n"
" run: %s\n"
" ingress:\n"
" - from:\n"
" - ipBlock:\n"
" cidr: 172.17.0.0/16\n"
" - podSelector:\n"
" matchLabels:\n"
" run: %s\n"
" ports:\n"
" - protocol: %s\n"
" port: %d\n"
" egress:\n"
" - to:\n"
" - ipBlock:\n"
" cidr: 10.0.0.0/24\n"
" ports:\n"
" - protocol: %s\n"
" port: %d\n";
// printf("----------TEMPLATE----------\n");
// printf(template, sourceaddr, destaddr, protocol, sourceport,
// protocol, destport); printf("\n----------------------------\n");
checkArgs(sourceaddr, &sourcetype, destaddr, &desttype);
FILE *outputFile = fopen(output, "w");
fprintf(outputFile, template, sourceaddr, destaddr, protocol,
sourceport, protocol, destport);
writeFile(output, sourceaddr, sourcetype, sourceport, destaddr,
desttype, destport, protocol);
fclose(outputFile);
char cmd[100];
sprintf(cmd, "kubectl apply -f ./%s", output);
printf("%s\n", cmd);
system(cmd);
// char cmd[100];
// sprintf(cmd, "kubectl apply -f ./%s", output);
//
// printf("%s\n", cmd);
// system(cmd);
return 0;
}