Skip to content

Commit

Permalink
Merge branch 'main' into smaller_ipblock
Browse files Browse the repository at this point in the history
  • Loading branch information
YairSlobodin1 authored Dec 11, 2024
2 parents 968e5c6 + 2be185b commit 007a252
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@f779452ac5af1c261dce0346a8f964149f49322b
uses: github/codeql-action/init@f09c1c0a94de965c15400f5634aa42fac8fb8f88
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -46,7 +46,7 @@ jobs:
# queries: ./path/to/local/query, your-org/your-repo/queries@main

- name: Autobuild
uses: github/codeql-action/autobuild@f779452ac5af1c261dce0346a8f964149f49322b
uses: github/codeql-action/autobuild@f09c1c0a94de965c15400f5634aa42fac8fb8f88

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@f779452ac5af1c261dce0346a8f964149f49322b
uses: github/codeql-action/analyze@f09c1c0a94de965c15400f5634aa42fac8fb8f88
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/np-guard/models

go 1.23
go 1.22

require github.com/stretchr/testify v1.9.0

Expand Down
10 changes: 10 additions & 0 deletions pkg/netset/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ func cidrToInterval(cidr string) (interval.Interval, error) {
return interval.New(int64(startNum), int64(endNum)), nil
}

// AsCidr returns the CIDR string of this IPBlock object, if it contains exactly one CIDR,
// otherwise it returns an error
func (b *IPBlock) AsCidr() (string, error) {
cidrList := b.ToCidrList()
if len(cidrList) != 1 {
return "", fmt.Errorf("ipblock contains %d cidrs", len(cidrList))
}
return cidrList[0], nil
}

// ToCidrList returns a list of CIDR strings for this IPBlock object
func (b *IPBlock) ToCidrList() []string {
var cidrList []string
Expand Down
9 changes: 9 additions & 0 deletions pkg/netset/ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ func TestConversions(t *testing.T) {
require.Equal(t, ipRange, toPrint[0])

require.Equal(t, "", ipb1.ToIPAddressString())

_, err = ipb1.AsCidr()
require.NotNil(t, err)

cidr := "5.2.1.0/24"
ipb3, _ := netset.IPBlockFromCidr(cidr)
str, err := ipb3.AsCidr()
require.Nil(t, err)
require.Equal(t, str, cidr)
}

func TestDisjointIPBlocks(t *testing.T) {
Expand Down

0 comments on commit 007a252

Please sign in to comment.