Skip to content

Commit

Permalink
Merge pull request #225 from kubernetes-simulator/extra-ips
Browse files Browse the repository at this point in the history
feat: allow extra ips to be added to the bastion sg
  • Loading branch information
raoulmillais authored Feb 25, 2020
2 parents 1b0920f + 42e5bf0 commit b61a05f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func newCreateCommand(logger *logrus.Logger) *cobra.Command {
tfDir := viper.GetString("tf-dir")
tfVarsDir := viper.GetString("tf-vars-dir")
disableIPDetection := viper.GetBool("disable-ip-detection")
extraCIDRs := viper.GetString("extra-cidrs")

logger.WithFields(logrus.Fields{
"BucketName": bucketName,
Expand All @@ -40,7 +41,8 @@ func newCreateCommand(logger *logrus.Logger) *cobra.Command {
sim.WithAttackRepo(attackRepo),
sim.WithBucketName(bucketName),
sim.WithoutIPDetection(disableIPDetection),
sim.WithTfVarsDir(tfVarsDir))
sim.WithTfVarsDir(tfVarsDir),
sim.WithExtraCIDRs(extraCIDRs))

err := simulator.Create()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func newCmdRoot() *cobra.Command {
panic(err)
}

rootCmd.PersistentFlags().StringP("extra-cidrs", "e", "",
"Extra CIDRs that will be allowed to access to the bastion host. MUST be a valid CIDR and a list MUST be comma delimited")
if err := viper.BindPFlag("extra-cidrs", rootCmd.PersistentFlags().Lookup("extra-cidrs")); err != nil {
panic(err)
}

// TODO: (rem) this is also used to locate the perturb.sh script which may be
// subsumed by this app
rootCmd.PersistentFlags().StringP("scenarios-dir", "s", "./simulation-scripts",
Expand Down
11 changes: 11 additions & 0 deletions pkg/simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Simulator struct {
ScenariosDir string
// disableIPDetection enables IP checks used for cidr access. Enabled by default.
DisableIPDetection bool
// Extra CIDRs to be added to the bastion security group to allow SSH from arbitrary
// locations
ExtraCIDRs string
}

// Option is a type used to configure a `Simulator` instance
Expand Down Expand Up @@ -116,3 +119,11 @@ func WithoutIPDetection(disableIPDetection bool) Option {
s.DisableIPDetection = disableIPDetection
}
}

// WithExtraCIDRs returns a configurer for creating a `Simulator` instance with
// `NewSimulator`
func WithExtraCIDRs(extraCIDRs string) Option {
return func(s *Simulator) {
s.ExtraCIDRs = extraCIDRs
}
}
3 changes: 2 additions & 1 deletion pkg/simulator/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func (s *Simulator) InitIfNeeded() error {
"PublicKey": publickey,
"AccessCIDR": accessCIDR,
"BucketName": s.BucketName,
"ExtraCIDRs": s.ExtraCIDRs,
}).Debug("Writing Terraform tfvars file")
err = EnsureLatestTfVarsFile(s.TfVarsDir, *publickey, accessCIDR, s.BucketName, s.AttackTag, s.AttackRepo)
err = EnsureLatestTfVarsFile(s.TfVarsDir, *publickey, accessCIDR, s.BucketName, s.AttackTag, s.AttackRepo, s.ExtraCIDRs)
if err != nil {
return errors.Wrap(err, "Error writing tfvars")
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/simulator/terraform_vars.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package simulator

import (
"strings"

"github.com/controlplaneio/simulator-standalone/pkg/util"
)

Expand All @@ -12,32 +14,42 @@ type TfVars struct {
BucketName string
AttackTag string
AttackRepo string
ExtraCIDRs string
}

// NewTfVars creates a TfVars struct with all the defaults
func NewTfVars(publicKey, accessCIDR, bucketName, attackTag, attackRepo string) TfVars {
func NewTfVars(publicKey, accessCIDR, bucketName, attackTag, attackRepo, extraCIDRs string) TfVars {
return TfVars{
PublicKey: publicKey,
AccessCIDR: accessCIDR,
BucketName: bucketName,
AttackTag: attackTag,
AttackRepo: attackRepo,
ExtraCIDRs: extraCIDRs,
}
}

func (tfv *TfVars) String() string {
if tfv.ExtraCIDRs != "" {
splitCIDRs := strings.Split(tfv.ExtraCIDRs, ",")
for i := range splitCIDRs {
splitCIDRs[i] = strings.TrimSpace(splitCIDRs[i])
}
templatedCIDRs := strings.Join(splitCIDRs, "\", \"")
tfv.AccessCIDR = tfv.AccessCIDR + "\", \"" + templatedCIDRs
}

return "access_key = \"" + tfv.PublicKey + "\"\n" +
"access_cidr = \"" + tfv.AccessCIDR + "\"\n" +
"access_cidr = [\"" + tfv.AccessCIDR + "\"]\n" +
"attack_container_tag = \"" + tfv.AttackTag + "\"\n" +
"attack_container_repo = \"" + tfv.AttackRepo + "\"\n" +
"state_bucket_name = \"" + tfv.BucketName + "\"\n"

}

// EnsureLatestTfVarsFile always writes an tfvars file
func EnsureLatestTfVarsFile(tfVarsDir, publicKey, accessCIDR, bucket, attackTag, attackRepo string) error {
func EnsureLatestTfVarsFile(tfVarsDir, publicKey, accessCIDR, bucket, attackTag, attackRepo, extraCIDRs string) error {
filename := tfVarsDir + "/settings/bastion.tfvars"
tfv := NewTfVars(publicKey, accessCIDR, bucket, attackTag, attackRepo)
tfv := NewTfVars(publicKey, accessCIDR, bucket, attackTag, attackRepo, extraCIDRs)

return util.OverwriteFile(filename, tfv.String())
}
8 changes: 4 additions & 4 deletions pkg/simulator/terraform_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
func Test_TfVars_String(t *testing.T) {
t.Parallel()
tfv := simulator.NewTfVars("ssh-rsa", "10.0.0.1/16", "test-bucket",
"latest", "controlplane/simulator-attack")
"latest", "controlplane/simulator-attack", "10.0.0.1/16")
expected := `access_key = "ssh-rsa"
access_cidr = "10.0.0.1/16"
access_cidr = ["10.0.0.1/16", "10.0.0.1/16"]
attack_container_tag = "latest"
attack_container_repo = "controlplane/simulator-attack"
state_bucket_name = "test-bucket"
Expand All @@ -36,10 +36,10 @@ func Test_Ensure_TfVarsFile_with_settings(t *testing.T) {
require.NoError(t, err)

err = simulator.EnsureLatestTfVarsFile(workDir, "ssh-rsa", "10.0.0.1/16",
"test-bucket", "latest", "controlplane/simulator-attack")
"test-bucket", "latest", "controlplane/simulator-attack", "10.0.0.1/16, 10.0.0.1/32")
require.NoError(t, err)
expected := `access_key = "ssh-rsa"
access_cidr = "10.0.0.1/16"
access_cidr = ["10.0.0.1/16", "10.0.0.1/16", "10.0.0.1/32"]
attack_container_tag = "latest"
attack_container_repo = "controlplane/simulator-attack"
state_bucket_name = "test-bucket"
Expand Down
1 change: 1 addition & 0 deletions terraform/deployments/AWS/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ variable "access_key" {

variable "access_cidr" {
description = "cidr range of client connection"
type = list(string)
}

// Variables below are to have defined defaults
Expand Down
34 changes: 20 additions & 14 deletions terraform/modules/AWS/SecurityGroups/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ resource "aws_security_group" "simulator_bastion_sg" {
name = "simulator-bastion-sg-${random_uuid.unique.result}"
vpc_id = var.vpc_id

ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = [var.access_cidr]
}

egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}

tags = merge(
var.default_tags,
{
Expand All @@ -33,6 +19,26 @@ resource "aws_security_group" "simulator_bastion_sg" {
)
}

resource "aws_security_group_rule" "allow_all_egress" {
type = "egress"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]

security_group_id = aws_security_group.simulator_bastion_sg.id
}

resource "aws_security_group_rule" "allow_user_ip_ssh" {
type = "ingress"
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = var.access_cidr

security_group_id = aws_security_group.simulator_bastion_sg.id
}

// Private subnet security group
// Restricts ingress from public subnet using ssh
// Egress open (via NAT for internet)
Expand Down
2 changes: 1 addition & 1 deletion terraform/modules/AWS/SecurityGroups/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
variable "access_cidr" {
description = "cidr range of client connection"
type = list(string)
}

variable "vpc_id" {
Expand All @@ -18,4 +19,3 @@ variable "default_tags" {
description = "Default tags for all resources"
type = map(string)
}

0 comments on commit b61a05f

Please sign in to comment.