Skip to content

Commit

Permalink
[G2-2] Some initial comments and formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Jun 19, 2022
1 parent bbca374 commit 6a619e4
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 108 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ $ make RIoTPot-build-local
$ ./RIoTPot
```

![Local Build](https://github.com/aau-network-security/RIoTPot/blob/master/internal/media/local_build.gif?raw=true)
![Local Build](assets/local_build.gif)

Upon running, user needs to select the mode of interaction, in Low mode, all plugins run locally as binaries, in High mode, the selected services run in separate container, and, in Hybrid mode, mix of Low and High i.e. some services locally and some inside containers.

In every mode, there is an option to run the services directly from reading the configuration file located at `config/samples/configuration.yml`

![Config file](https://github.com/aau-network-security/RIoTPot/blob/master/internal/media/configuration_file.png?raw=true)
![Config file](assets/configuration_file.png)

By editing the `boot_plugins` tag, services to run as binaries inside can be provided, see `emulators` tag in the same configuration file to input allowed service plugins only

Expand All @@ -162,7 +162,7 @@ $ cd RIoTPot/deployments
$ go run interactive_deployer.go
```

![Containerized Build](https://github.com/aau-network-security/RIoTPot/blob/master/internal/media/containerized_build.gif?raw=true)
![Containerized Build](assets/containerized_build.gif)

Upon choosing modes and services correctly, following message will be displayed:

Expand All @@ -189,7 +189,7 @@ $ docker ps

One can also setup the Containerized RIoTPot through config file located at, `config/samples/configuration.yml`

![Config file](https://github.com/aau-network-security/RIoTPot/blob/master/internal/media/configuration_file.png?raw=true)
![Config file](assets/configuration_file.png)

By editing the `boot_plugins` tag, services to run as binaries inside can be provided, see `emulators` tag in the same configuration file to input allowed service plugins only

Expand Down Expand Up @@ -225,7 +225,7 @@ $ docker run -d RIoTPot-docker:latest

## 4. Documentation

The documentation for RIoTPot can be found in [go.pkg.dev](https://pkg.go.dev/), however, sometimes you might be in need to visualize the documentation locally, either because you are developing a part of it, of for any other reason.
The documentation for RIoTPot can be found in [go.pkg.dev](https://pkg.go.dev/), however, sometimes you might be in need to visualize the documentation locally, either because you are developing a part of it, or for any other reason.

The most common way of pre-visualizing documentation is by using `godoc`, however, this requires an initial setup of the go project. Find more information in the [godoc page](https://pkg.go.dev/golang.org/x/tools/cmd/godoc).

Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
114 changes: 60 additions & 54 deletions internal/configuration/autopilot.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package configuration

import (
"os"
"bufio"
"fmt"
"log"
"net"
"sync"
"bufio"
"os"
"strconv"
"strings"
"sync"


"github.com/riotpot/pkg/services"
"github.com/riotpot/internal/greeting"
"go.mongodb.org/mongo-driver/mongo"
"github.com/riotpot/pkg/profiles/ports"
"github.com/riotpot/pkg/services"
"github.com/riotpot/tools/arrays"
"github.com/riotpot/tools/environ"
"github.com/riotpot/pkg/profiles/ports"
"go.mongodb.org/mongo-driver/mongo"
)

type Autopilot struct {
Settings Settings
Profile Profile

greeting greeting.Greet
services services.Services
wg sync.WaitGroup
DB *mongo.Client
loaded_plugins []string
greeting greeting.Greet
services services.Services
wg sync.WaitGroup
DB *mongo.Client
loaded_plugins []string
loaded_containers []string
plugins_to_run []string
containers_to_run [] string
interaction_mode string
remote_host_ip string
docker_context_name string
plugins_to_run []string
containers_to_run []string
interaction_mode string
remote_host_ip string
//docker_context_name string
}

// Method to start the autopilot.
Expand All @@ -59,7 +58,7 @@ func (a *Autopilot) Start() {

// check if the build is local or containerized
if a.Settings.Riotpot.Local_build_on == "1" { // User is running the Riotpot in local build mode

// check if user want to run via config file or manually input
a.interaction_mode = a.CheckInteractionMode()

Expand All @@ -68,8 +67,8 @@ func (a *Autopilot) Start() {
running_mode_decision := a.CheckRunningMode()

if running_mode_decision == "manual" { // User has decided to provide plugins to run in interactive/manual way
// user needs to choose which plugins to choose from this list

// user needs to choose which plugins to choose from this list
fmt.Printf("Plugins available to run are ")
fmt.Println(a.loaded_plugins)

Expand Down Expand Up @@ -107,7 +106,7 @@ func (a *Autopilot) Start() {
log.Fatalf("\nPlease check the config file, and try again\n")
}

// check if the port which containers require to run is free on host machine
// check if the port which containers require to run is free on host machine
a.CheckContainerPort()
a.DeployContainers()
}
Expand All @@ -119,7 +118,7 @@ func (a *Autopilot) Start() {
fmt.Println(a.loaded_plugins)
fmt.Printf("\n")
a.plugins_to_run = a.GetPluginsFromUser()

fmt.Printf("\nDocker containers available to run are ")
fmt.Println(a.loaded_containers)
fmt.Printf("\n")
Expand Down Expand Up @@ -157,7 +156,7 @@ func (a *Autopilot) Start() {
a.plugins_to_run = arrays.StringToArray(strings.ToLower(arrays.ArrayToString(a.plugins_to_run)))

if !a.ValidatePlugin(a.plugins_to_run) {
log.Fatalf("\nPlease check the config file, and try again\n")
log.Fatalf("\nPlease check the config file, and try again\n")
}
} else if a.Settings.Riotpot.Mode == "high" {
a.plugins_to_run = nil
Expand All @@ -166,7 +165,7 @@ func (a *Autopilot) Start() {
fmt.Println(a.Settings.Riotpot.Start_images)

if !a.ValidateContainers(a.containers_to_run) {
log.Fatalf("\nPlease check the config file, and try again\n")
log.Fatalf("\nPlease check the config file, and try again\n")
}
a.containers_to_run = arrays.StringToArray(strings.ToLower(arrays.ArrayToString(a.containers_to_run)))
// glider forwards all the traffic on specific port to the respective service container
Expand All @@ -180,16 +179,16 @@ func (a *Autopilot) Start() {
fmt.Println(a.Settings.Riotpot.Start_images)

if !a.ValidatePlugin(a.plugins_to_run) {
log.Fatalf("\nPlease check the config file, and try again\n")
log.Fatalf("\nPlease check the config file, and try again\n")
}
a.plugins_to_run = arrays.StringToArray(strings.ToLower(arrays.ArrayToString(a.plugins_to_run)))

if !a.ValidateContainers(a.containers_to_run) {
log.Fatalf("\nPlease check the config file, and try again\n")
log.Fatalf("\nPlease check the config file, and try again\n")
}

a.containers_to_run = arrays.StringToArray(strings.ToLower(arrays.ArrayToString(a.containers_to_run)))

a.DeployGlider()
}
}
Expand Down Expand Up @@ -226,7 +225,7 @@ func (a *Autopilot) available(name string, port int) (available bool) {
return true
}

// check which interaction mode in supplied by the user from config file
// check which interaction mode in supplied by the user from config file
func (a *Autopilot) CheckModesFromConfig() {
mode_received := arrays.StringToArray(a.Settings.Riotpot.Mode)

Expand All @@ -236,7 +235,7 @@ func (a *Autopilot) CheckModesFromConfig() {
log.Fatalf("\nPlease enter atleast one mode in Riotpot config mode, i.e. low, high or hybrid\n")
}

if ! arrays.Contains(a.Settings.Riotpot.Allowed_modes, mode_received[0]) {
if !arrays.Contains(a.Settings.Riotpot.Allowed_modes, mode_received[0]) {
log.Fatalf("\n %q mode is invalid, only choose low, high or hybrid mode only in Riotpot config\n", mode_received[0])
}
}
Expand All @@ -262,8 +261,9 @@ func (a *Autopilot) DiscoverImages() {

// Displays which is the current Riotpot running mode, i.e. low, high or hybrid
func (a *Autopilot) DiscoverRunningMode() {
mode := "[+] Current mode of running is "+ a.Settings.Riotpot.Mode+"\n"
fmt.Printf(mode)
str_mode := "[+] Current mode of running is %s"
mode := a.Settings.Riotpot.Mode
fmt.Printf(str_mode, mode)
}

// Load the greeting
Expand Down Expand Up @@ -292,14 +292,20 @@ func (a *Autopilot) ReadInput() (text string) {
return text
}

// Check if the port of container to run is available for listening
// TO-DO: print all the invalid containers not just the first one encountered
// Check if the port of container to run is available for listening
func (a *Autopilot) CheckContainerPort() {
for _, container := range a.containers_to_run {

// Get the port and the protocol of the container
port := ports.GetPort(arrays.AddSuffix(container, "d"))
// Convert the Port from int to string
if environ.CheckPortBusy(ports.GetProtocol(arrays.AddSuffix(container, "d")) , strconv.Itoa(port)) == false {
log.Fatalf("[-] Port %d of Container %q is already busy on host, please free it first!", port, container)
// Convert the port to a string
port_st := strconv.Itoa(port)
protocol := ports.GetProtocol(arrays.AddSuffix(container, "d"))

// Check if the protocol in the machine in that port is busy
isbusy := environ.CheckPortBusy(protocol, port_st)
if isbusy {
log.Fatalf("[-] Port %s of Container %q is already busy on host, please free it first!", port_st, container)
}
}
}
Expand All @@ -310,31 +316,31 @@ func (a *Autopilot) DeployContainers() {
uri := a.Settings.GetContainerURI(container)
// get the port number of a given container, currently all container must have an entry in ports file
port := strconv.Itoa(ports.GetPort(arrays.AddSuffix(container, "d")))
port_mapping := port+":"+port
port_mapping := port + ":" + port
app := environ.GetPath("docker")
environ.ExecuteBackgroundCmd(app, "run", "-p", port_mapping, uri )
environ.ExecuteBackgroundCmd(app, "run", "-p", port_mapping, uri)
fmt.Printf("\nContainer %q, deployed \n", container)
}
}

// Port forwarding using glider for container ports
// Port forwarding using glider for container ports
func (a *Autopilot) DeployGlider() {
for _, container := range a.containers_to_run {
port := strconv.Itoa(ports.GetPort(arrays.AddSuffix(container, "d")))
protocol := ports.GetProtocol(arrays.AddSuffix(container, "d"))
listener := protocol+"://:"+port
listener := protocol + "://:" + port
remote_ip := a.Settings.GetContainerIP(container)
forwarder := protocol+"://"+ remote_ip +":"+port
forwarder := protocol + "://" + remote_ip + ":" + port
fmt.Println(a.remote_host_ip)

app := environ.GetPath("glider")

environ.ExecuteBackgroundCmd(app, "-verbose", "-listen", listener, "-forward", forwarder)
}
}

// Interactively checks if the user wants to provide plugins to run manually
func (a *Autopilot) CheckRunningMode() (string) {
func (a *Autopilot) CheckRunningMode() string {
fmt.Print("Run plugins from configuation file? [y/n]")

for {
Expand All @@ -345,7 +351,7 @@ func (a *Autopilot) CheckRunningMode() (string) {
return "config"
} else if response == "n" || response == "no" {
return "manual"
} else{
} else {
fmt.Printf("Please type Yes(y) or No(n) only\n")
}
}
Expand All @@ -365,16 +371,16 @@ func (a *Autopilot) CheckInteractionMode() (decision string) {
return "high"
} else if response == "hy" || response == "hybrid" {
return "hybrid"
} else{
} else {
fmt.Printf("Please type low(l) or high(h) or hybrid(hy) only\n")
}
}
}

// Validates if the plugins to run matches the available plugins
// TODO: print all the invalid plugins not just the first one encountered
func (a *Autopilot) ValidatePlugin(in_plugins []string) (validated bool){
if arrays.HaveDuplicateItems(in_plugins){
func (a *Autopilot) ValidatePlugin(in_plugins []string) (validated bool) {
if arrays.HaveDuplicateItems(in_plugins) {
fmt.Printf("\n[-] Entered plugins has duplicate entries, please enter again\n")
return false
}
Expand All @@ -393,14 +399,14 @@ func (a *Autopilot) ValidatePlugin(in_plugins []string) (validated bool){

// Validates if the containers to run matches the loaded containers
// TODO: print all the invalid containers not just the first one encountered
func (a *Autopilot) ValidateContainers(in_containers []string) (validated bool){
if arrays.HaveDuplicateItems(in_containers){
func (a *Autopilot) ValidateContainers(in_containers []string) (validated bool) {
if arrays.HaveDuplicateItems(in_containers) {
fmt.Printf("\n[-] Entered containers has duplicate entries, please enter again\n")
return false
}

for _, container := range in_containers {
validated := arrays.Contains( a.loaded_containers, container)
validated := arrays.Contains(a.loaded_containers, container)
if !validated {
fmt.Printf("\n[-] Entered container \"%s\" doesn't exist, please enter plugins again... \n", container)
return false
Expand Down Expand Up @@ -459,13 +465,13 @@ func (a *Autopilot) SetLoadedPlugins() {
}

// Validates if the given docker context exists and if it is set to default
func (a *Autopilot) ValidateDefaultDockerContext(to_check string) {
func (a *Autopilot) ValidateDefaultDockerContext(to_check string) {
path := environ.GetPath("docker")
cmd_output := environ.ExecuteCmd(path, "context" , "ls")
cmd_output := environ.ExecuteCmd(path, "context", "ls")
cmd_out_slice := arrays.StringToArray(cmd_output)
val_position := arrays.GetItemPosition(cmd_out_slice, to_check)

if val_position== -1 {
if val_position == -1 {
log.Fatalf("Docker context %q, not found", to_check)
}

Expand Down
15 changes: 7 additions & 8 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"strconv"
"strings"

"gopkg.in/yaml.v3"
"github.com/gobuffalo/packr"
errors "github.com/riotpot/tools/errors"
arrays "github.com/riotpot/tools/arrays"
environ "github.com/riotpot/tools/environ"

errors "github.com/riotpot/tools/errors"
"gopkg.in/yaml.v3"
)

func NewSettings() (s Settings, err error) {
Expand Down Expand Up @@ -44,7 +43,7 @@ type Settings struct {
// Load the configuration on the child.
func (conf *Settings) Load() (err error) {
box := packr.NewBox("../../configs/samples")
data, err := box.Find("configuration.yml")
data, _ := box.Find("configuration.yml")

err = yaml.Unmarshal(data, &conf)
errors.Raise(err)
Expand All @@ -62,7 +61,7 @@ func (conf *Settings) GetDockerImages() (images []string) {
}

// Retrieve the image name from Start_images tag in configuration file
func (conf *Settings) GetDockerImagesToRun() ([]string) {
func (conf *Settings) GetDockerImagesToRun() []string {
// for _, val := range conf.Riotpot.Start_images {
// images = append(images, strings.TrimSuffix(arrays.StringToArray(val)[0], ","))
// }
Expand Down Expand Up @@ -173,7 +172,7 @@ func (conf *Settings) ResolveEnv() {
}

db_cfg.Identity.Name = environ.Getenv("DB_NAME", db_cfg.Identity.Name)

conf.Databases[0] = db_cfg
}

Expand Down Expand Up @@ -205,8 +204,8 @@ type ConfigRiotpot struct {
Images []string
// Interaction mode of Riotpot, used in containazried build
Mode string
// Modes of operation which are currently supported by Riotpot
Allowed_modes [] string
// Modes of operation which are currently supported by Riotpot
Allowed_modes []string
// Container images which are finalized to run in the Riotpot run
Start_images string
}
Expand Down
Loading

0 comments on commit 6a619e4

Please sign in to comment.