#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, "Usage:\t %s sourceip sourceport desinationip " "destinationport protocol [-o outputfile]\n", argv[0]); fprintf(stderr, "\tor %s sourcepod sourceport desinationpod " "destinationport protocol [-o outputfile]\n", argv[0]); fprintf(stderr, "\nExample:\t%s nginx 80 access 5978 TCP " "-o test.yaml\n", argv[0]); fprintf(stderr, "\t\t%s nginx 80 access 5978 TCP\n", argv[0]); return EX_USAGE; } int opt; char output[200] = ""; while ((opt = getopt(argc, argv, "o:")) != -1) { switch (opt) { case 'o': strcpy(output, optarg); break; } } if (strlen(output) == 0) { strcpy(output, "ingress-egress-nginx.yaml"); } char *sourceaddr = argv[optind]; int sourceport; sscanf(argv[optind + 1], "%d", &sourceport); char *destaddr = argv[optind + 2]; int destport; sscanf(argv[optind + 3], "%d", &destport); char *protocol = argv[optind + 4]; int sourcetype, desttype; checkArgs(sourceaddr, &sourcetype, destaddr, &desttype); writeFile(output, sourceaddr, sourcetype, sourceport, destaddr, desttype, destport, protocol); // char cmd[100]; // sprintf(cmd, "kubectl apply -f ./%s", output); // // printf("%s\n", cmd); // system(cmd); return 0; }