Skip to content

Commit 35fe663

Browse files
Merge pull request #11 from piyushsonigra/fix/rule-not-exist-error
fix: sg rule doesnt exist error
2 parents 6075ea6 + e4e8c20 commit 35fe663

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Build and Release aws_ipadd
33
on:
44
push:
55
tags:
6-
- 'v*'
6+
- "v*"
77
pull_request:
88
branches:
99
- main
@@ -26,7 +26,7 @@ jobs:
2626

2727
- name: Build
2828
run: |
29-
make build
29+
RELEASE_VERSION="${{ github.ref_name }}" make build
3030
3131
- name: Archive Build Artifact
3232
if: github.ref_type == 'tag'

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
BUILD_DIR := dist
22
BIN_NAME := aws_ipadd
33
PLATFORMS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64
4+
RELEASE_VERSION ?= dev
45

56
clean:
67
@rm -rf $(BUILD_DIR)
@@ -12,8 +13,8 @@ build: clean
1213
ARCH=$$(echo $$platform | cut -d'/' -f2); \
1314
OUTPUT_FILE=$(BIN_NAME); \
1415
ARTF_FILE=$(BIN_NAME)_$${OS}_$${ARCH}; \
15-
echo "Building $$OUTPUT_FILE..."; \
16-
GOOS=$$OS GOARCH=$$ARCH go build -o $(BUILD_DIR)/$$OUTPUT_FILE .; \
16+
echo "Building $$ARTF_FILE ..."; \
17+
GOOS=$$OS GOARCH=$$ARCH go build -ldflags "-X aws_ipadd/cliargs.Version=$(RELEASE_VERSION)" -o $(BUILD_DIR)/$$OUTPUT_FILE .; \
1718
tar -czf $(BUILD_DIR)/$$ARTF_FILE.tar.gz -C $(BUILD_DIR) $$OUTPUT_FILE; \
1819
rm -f $(BUILD_DIR)/$$OUTPUT_FILE; \
1920
done

cliargs/cliargs.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"os"
77
)
88

9+
// Version will be set during build time
10+
var Version = "dev"
11+
912
// Args stores command-line arguments.
1013
type Args struct {
1114
Profile string
@@ -30,13 +33,20 @@ func customUsage() {
3033
fmt.Println(" --protocol <int> Protocol e.g TCP, UPD, all (optional)")
3134
fmt.Println(" --ip <string> IP address with subnetmask e.g '10.10.19.1/32' (optional)")
3235
fmt.Println(" --rule_name <string> Security group rule name (optional)")
36+
fmt.Println(" --help, -h <string> Show help")
37+
fmt.Println(" --version, -v <string> Show aws_ipadd version")
3338
}
3439

3540
// Parse CLI Arguments
3641
func ParseArgs() *Args {
3742
args := &Args{}
3843
flag.Usage = customUsage
3944

45+
// Add version flag
46+
versionFlag := flag.Bool("version", false, "Show aws_ipadd version")
47+
// Add short version flag
48+
vShort := flag.Bool("v", false, "Show aws_ipadd version")
49+
4050
flag.StringVar(&args.Profile, "profile", "", "AWS profile name (required)")
4151
flag.StringVar(&args.Port, "port", "", "Port number, this will be ignored if from_port and to_port is passed (optional)")
4252
flag.StringVar(&args.FromPort, "from_port", "", "Port number, It should be passed with to_port. Only from_port is not valid argument (optional)")
@@ -47,6 +57,13 @@ func ParseArgs() *Args {
4757

4858
flag.Parse()
4959

60+
// Check for version
61+
if *versionFlag || *vShort {
62+
fmt.Printf("aws_ipadd version %s\n", Version)
63+
os.Exit(0)
64+
}
65+
66+
// Check for profile
5067
if args.Profile == "" {
5168
fmt.Println("Error: --profile is required")
5269
flag.Usage()

configloader/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ func GetConfig(section *ini.Section, args *cliargs.Args) (*SecurityGroupRule, er
7070

7171
// Set rule name
7272
if args.RuleName != "" {
73-
rule.RuleName = args.RuleName
73+
rule.RuleName = strings.TrimSpace(args.RuleName)
7474
} else {
75-
rule.RuleName = section.Key("rule_name").String()
75+
rule.RuleName = strings.TrimSpace(section.Key("rule_name").String())
7676
}
7777

7878
return rule, nil

securitygroup/rule.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,18 @@ func ProcessRule(profile string, ruleConfig *configloader.SecurityGroupRule) (st
6565

6666
// Modify security group rule
6767
if ruleNameMatched {
68-
fmt.Println("Whitelisting your current IP...")
6968
// Revoke old IP permission
70-
res, err := revokeIPPermission(ec2Client, &ruleConfig.SecurityGroupID, newRule, *matchingRule.IpRanges[0].CidrIp)
71-
if err != nil {
72-
return "", err
69+
if ruleConfig.RuleName != "" {
70+
fmt.Println("Updating your current IP...")
71+
res, err := revokeIPPermission(ec2Client, &ruleConfig.SecurityGroupID, newRule, *matchingRule.IpRanges[0].CidrIp)
72+
if err != nil {
73+
return "", err
74+
}
75+
fmt.Println(res)
7376
}
74-
fmt.Println(res)
7577

7678
// Allow new IP permission
77-
res, err = allowIPPermission(ec2Client, &ruleConfig.SecurityGroupID, newRule, &ruleConfig.IP)
79+
res, err := allowIPPermission(ec2Client, &ruleConfig.SecurityGroupID, newRule, &ruleConfig.IP)
7880
if err != nil {
7981
return "", err
8082
}

0 commit comments

Comments
 (0)