From c3e4c03dec116a1d422a70638b08ce8a0c6a6fdf Mon Sep 17 00:00:00 2001 From: Jagraj Aulakh Date: Tue, 31 Jan 2023 15:10:51 -0500 Subject: [PATCH] Matches source and destination addresses with IP regex, and uses the appropriate selector in the YAML output --- Makefile | 2 +- README.md | 16 ++++++ mkconfig.c | 140 ++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 README.md diff --git a/Makefile b/Makefile index 77f0aaf..f17eb25 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/README.md b/README.md new file mode 100644 index 0000000..b695214 --- /dev/null +++ b/README.md @@ -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 +``` + diff --git a/mkconfig.c b/mkconfig.c index 7853c6b..f960d6d 100644 --- a/mkconfig.c +++ b/mkconfig.c @@ -1,9 +1,96 @@ +#include #include #include #include #include #include +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; }