Skip to content

Commit

Permalink
Merge pull request #87 from Chia-Network/trusted-cidrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Starttoaster committed May 31, 2024
2 parents a25a4cf + 3890915 commit 1e13e42
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 2 deletions.
5 changes: 5 additions & 0 deletions api/v1/chianode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type ChiaNodeSpec struct {
// ChiaNodeSpecChia defines the desired state of Chia component configuration
type ChiaNodeSpecChia struct {
CommonSpecChia `json:",inline"`

// TrustedCIDRs is a list of CIDRs that this chia component should trust peers from
// See: https://docs.chia.net/faq/?_highlight=trust#what-are-trusted-peers-and-how-do-i-add-them
// +optional
TrustedCIDRs *[]string `json:"trustedCIDRs,omitempty"`
}

// ChiaNodeStatus defines the observed state of ChiaNode
Expand Down
8 changes: 8 additions & 0 deletions api/v1/chianode_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ spec:
dnsIntroducerAddress: dns-introducer.svc.cluster.local
timezone: "UTC"
logLevel: "INFO"
trustedCIDRs:
- "192.168.0.0/16"
- "10.0.0.0/8"
chiaExporter:
enabled: true
serviceLabels:
Expand All @@ -48,6 +51,10 @@ spec:
introducerAddress = "introducer.svc.cluster.local"
dnsIntroducerAddress = "dns-introducer.svc.cluster.local"
)
expectCIDRs := []string{
"192.168.0.0/16",
"10.0.0.0/8",
}
expect := ChiaNode{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Expand All @@ -74,6 +81,7 @@ spec:
Timezone: &timezone,
LogLevel: &logLevel,
},
TrustedCIDRs: &expectCIDRs,
},
CommonSpec: CommonSpec{
ChiaExporterConfig: SpecChiaExporter{
Expand Down
5 changes: 5 additions & 0 deletions api/v1/chiawallet_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type ChiaWalletSpecChia struct {
// In Kubernetes this is likely to be <node service name>.<namespace>.svc.cluster.local:8555
// +optional
FullNodePeer string `json:"fullNodePeer,omitempty"`

// TrustedCIDRs is a list of CIDRs that this chia component should trust peers from
// See: https://docs.chia.net/faq/?_highlight=trust#what-are-trusted-peers-and-how-do-i-add-them
// +optional
TrustedCIDRs *[]string `json:"trustedCIDRs,omitempty"`
}

// ChiaWalletStatus defines the observed state of ChiaWallet
Expand Down
8 changes: 8 additions & 0 deletions api/v1/chiawallet_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ spec:
secretKey:
name: "chiakey-secret"
key: "key.txt"
trustedCIDRs:
- "192.168.0.0/16"
- "10.0.0.0/8"
chiaExporter:
enabled: true
serviceLabels:
Expand All @@ -52,6 +55,10 @@ spec:
introducerAddress = "introducer.svc.cluster.local"
dnsIntroducerAddress = "dns-introducer.svc.cluster.local"
)
expectCIDRs := []string{
"192.168.0.0/16",
"10.0.0.0/8",
}
expect := ChiaWallet{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Expand Down Expand Up @@ -83,6 +90,7 @@ spec:
Name: "chiakey-secret",
Key: "key.txt",
},
TrustedCIDRs: &expectCIDRs,
},
CommonSpec: CommonSpec{
ChiaExporterConfig: SpecChiaExporter{
Expand Down
18 changes: 18 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/bases/k8s.chia.net_chianodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,13 @@ spec:
description: Timezone can be set to your local timezone for accurate
timestamps. Defaults to UTC
type: string
trustedCIDRs:
description: |-
TrustedCIDRs is a list of CIDRs that this chia component should trust peers from
See: https://docs.chia.net/faq/?_highlight=trust#what-are-trusted-peers-and-how-do-i-add-them
items:
type: string
type: array
required:
- caSecretName
type: object
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/k8s.chia.net_chiawallets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,13 @@ spec:
description: Timezone can be set to your local timezone for accurate
timestamps. Defaults to UTC
type: string
trustedCIDRs:
description: |-
TrustedCIDRs is a list of CIDRs that this chia component should trust peers from
See: https://docs.chia.net/faq/?_highlight=trust#what-are-trusted-peers-and-how-do-i-add-them
items:
type: string
type: array
required:
- caSecretName
- secretKey
Expand Down
18 changes: 18 additions & 0 deletions internal/controller/chianode/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package chianode

import (
"context"
"encoding/json"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/log"
"strconv"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -101,6 +104,7 @@ func (r *ChiaNodeReconciler) getChiaVolumeMounts(ctx context.Context, node k8sch

// getChiaNodeEnv retrieves the environment variables from the Chia config struct
func (r *ChiaNodeReconciler) getChiaNodeEnv(ctx context.Context, node k8schianetv1.ChiaNode) []corev1.EnvVar {
logr := log.FromContext(ctx)
var env []corev1.EnvVar

// service env var
Expand Down Expand Up @@ -167,6 +171,20 @@ func (r *ChiaNodeReconciler) getChiaNodeEnv(ctx context.Context, node k8schianet
})
}

// trusted_cidrs env var
if node.Spec.ChiaConfig.TrustedCIDRs != nil {
// TODO should any special CIDR input checking happen here
cidrs, err := json.Marshal(*node.Spec.ChiaConfig.TrustedCIDRs)
if err != nil {
logr.Error(err, fmt.Sprintf("ChiaNodeReconciler ChiaNode=%s given CIDRs could not be marshalled to json. Peer connections that you would expect to be trusted might not be trusted.", node.Name))
} else {
env = append(env, corev1.EnvVar{
Name: "trusted_cidrs",
Value: string(cidrs),
})
}
}

// TZ env var
if node.Spec.ChiaConfig.Timezone != nil {
env = append(env, corev1.EnvVar{
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/chiaseeder/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ package chiaseeder
import (
"context"
"fmt"
"strconv"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strconv"

k8schianetv1 "github.com/chia-network/chia-operator/api/v1"
"github.com/chia-network/chia-operator/internal/controller/common/consts"
Expand Down
17 changes: 17 additions & 0 deletions internal/controller/chiawallet/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package chiawallet

import (
"context"
"encoding/json"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/log"
"strconv"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -85,6 +87,7 @@ func (r *ChiaWalletReconciler) getChiaVolumes(ctx context.Context, wallet k8schi

// getChiaEnv retrieves the environment variables from the Chia config struct
func (r *ChiaWalletReconciler) getChiaEnv(ctx context.Context, wallet k8schianetv1.ChiaWallet) []corev1.EnvVar {
logr := log.FromContext(ctx)
var env []corev1.EnvVar

// service env var
Expand Down Expand Up @@ -145,6 +148,20 @@ func (r *ChiaWalletReconciler) getChiaEnv(ctx context.Context, wallet k8schianet
})
}

// trusted_cidrs env var
if wallet.Spec.ChiaConfig.TrustedCIDRs != nil {
// TODO should any special CIDR input checking happen here
cidrs, err := json.Marshal(*wallet.Spec.ChiaConfig.TrustedCIDRs)
if err != nil {
logr.Error(err, fmt.Sprintf("ChiaWalletReconciler ChiaWallet=%s given CIDRs could not be marshalled to json. Peer connections that you would expect to be trusted might not be trusted.", wallet.Name))
} else {
env = append(env, corev1.EnvVar{
Name: "trusted_cidrs",
Value: string(cidrs),
})
}
}

// TZ env var
if wallet.Spec.ChiaConfig.Timezone != nil {
env = append(env, corev1.EnvVar{
Expand Down

0 comments on commit 1e13e42

Please sign in to comment.