-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
210 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package libkube | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"regexp" | ||
|
||
discoveryv1 "k8s.io/api/discovery/v1" | ||
) | ||
|
||
const ( | ||
AddressFQDNMatcher = `^(([a-z0-9][a-z0-9\-]*[a-z0-9])|[a-z0-9]+\.)*([a-z]+|xn\-\-[a-z0-9]+)\.?$` | ||
) | ||
|
||
// AddressType returns the discoveryv1.AddressType identifier corresponding to the provided address. | ||
func AddressType(address string) (discoveryv1.AddressType, error) { | ||
ip := net.ParseIP(address) | ||
switch { | ||
case ip.To4() != nil: | ||
return discoveryv1.AddressTypeIPv4, nil | ||
case ip.To16() != nil: | ||
return discoveryv1.AddressTypeIPv6, nil | ||
} | ||
|
||
if match, _ := regexp.MatchString(AddressFQDNMatcher, address); match { | ||
return discoveryv1.AddressTypeFQDN, nil | ||
} | ||
|
||
return "", fmt.Errorf("invalid addrress input: %s", address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package libkube | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
discoveryv1 "k8s.io/api/discovery/v1" | ||
) | ||
|
||
func TestAddressType(t *testing.T) { | ||
type args struct { | ||
address string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want discoveryv1.AddressType | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success case IPv4", | ||
args: args{address: "10.1.1.1"}, | ||
want: discoveryv1.AddressTypeIPv4, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "success case IPv6", | ||
args: args{address: "fe80::857:d999:5316:78d4"}, | ||
want: discoveryv1.AddressTypeIPv6, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "success case FQDN", | ||
args: args{address: "a.domain.local"}, | ||
want: discoveryv1.AddressTypeFQDN, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "error case", | ||
args: args{address: "a.domain.local OR NOT!"}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := AddressType(tt.args.address) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("AddressType() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("AddressType() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,20 @@ | ||
package libkube | ||
|
||
import ( | ||
"strings" | ||
"fmt" | ||
|
||
"github.com/DataDog/KubeHound/pkg/globals/types" | ||
) | ||
|
||
// ServiceName returns the name of the service associated with the provided EndpointSlice. | ||
func ServiceName(ep types.EndpointType) string { | ||
return ep.Labels["kubernetes.io/service-name"] | ||
} | ||
|
||
// TODO service DNS name | ||
// ServiceDns provides the DNS name of the service associated with the provided EndpointSlice. | ||
func ServiceDns(ep types.EndpointType) string { | ||
var sb strings.Builder | ||
clusterName := "stripe.us1.staging.dog" // TODO dynamic | ||
namespace := ep.Namespace | ||
|
||
sb.WriteString(ep.Labels["kubernetes.io/service-name"]) | ||
sb.WriteString(".") | ||
sb.WriteString("stripe.us1.staging.dog") | ||
|
||
return sb.String() | ||
return fmt.Sprintf("%s.%s.%s", ep.Labels["kubernetes.io/service-name"], namespace, clusterName) | ||
} | ||
|
||
// // Exposed port from containerPort | ||
// func ExposedPort() int { | ||
|
||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.