Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.

Commit

Permalink
Add firewall configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Vittegleo committed Jun 6, 2017
1 parent 87158ea commit e5abd63
Show file tree
Hide file tree
Showing 41 changed files with 1,652 additions and 274 deletions.
100 changes: 100 additions & 0 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"fmt"
"log"
"sort"
"strings"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -44,6 +47,46 @@ type options struct {
dropletSize string
}

func RemoveAllDroplets(token string) ([]string, error) {
oauthClient := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client := godo.NewClient(oauthClient)

droplets, _, err := client.Droplets.List(context.TODO(), nil)
if err != nil {
return nil, err
}

// attempt removal of all dosxvpn droplets
removedDroplets := make([]string, 0)
for _, droplet := range droplets {
if strings.Contains(droplet.Name, "dosxvpn") {
_, err := client.Droplets.Delete(context.TODO(), droplet.ID)
if err != nil {
log.Println("Failed to remove droplet", droplet.Name, err)
}
removedDroplets = append(removedDroplets, droplet.Name)
}
}
sort.Strings(removedDroplets)

// attempt removal of all dosxvpn firewalls
firewalls, _, err := client.Firewalls.List(context.TODO(), nil)
if err == nil {
for _, firewall := range firewalls {
if strings.Contains(firewall.Name, "dosxvpn") {
_, err := client.Firewalls.Delete(context.TODO(), firewall.ID)
if err != nil {
log.Println("Failed to remove firewall", firewall.Name, err)
}
}
}
}

return removedDroplets, nil
}

func Deploy(accessToken string, opts ...Option) (*Droplet, error) {
opt := options{
dropletName: dropletName,
Expand Down Expand Up @@ -127,5 +170,62 @@ func Deploy(accessToken string, opts ...Option) (*Droplet, error) {
return nil, fmt.Errorf("timeout waiting for provisioning of droplet %d", droplet.DropletID)
}
}

fwRequest := &godo.FirewallRequest{
Name: opt.dropletName,
InboundRules: []godo.InboundRule{
{
Protocol: "tcp",
PortRange: "22",
Sources: &godo.Sources{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
{
Protocol: "udp",
PortRange: "500",
Sources: &godo.Sources{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
{
Protocol: "udp",
PortRange: "4500",
Sources: &godo.Sources{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
},
OutboundRules: []godo.OutboundRule{
{
Protocol: "icmp",
Destinations: &godo.Destinations{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
{
Protocol: "tcp",
PortRange: "all",
Destinations: &godo.Destinations{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
{
Protocol: "udp",
PortRange: "all",
Destinations: &godo.Destinations{
Addresses: []string{"0.0.0.0/0", "::/0"},
},
},
},
DropletIDs: []int{d.ID},
}

// Setup firewall
_, _, err = client.Firewalls.Create(context.TODO(), fwRequest)
if err != nil {
return nil, err
}

return droplet, nil
}
7 changes: 5 additions & 2 deletions glide.lock

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

18 changes: 3 additions & 15 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -164,21 +163,10 @@ func (h *handler) uninstall(rw http.ResponseWriter, req *http.Request) {
return
}

oauthClient := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client := godo.NewClient(oauthClient)

droplets, _, err := client.Droplets.List(context.TODO(), nil)

removedDroplets := make([]string, 0)
for _, droplet := range droplets {
if strings.Contains(droplet.Name, "dosxvpn") {
client.Droplets.Delete(context.TODO(), droplet.ID)
removedDroplets = append(removedDroplets, droplet.Name)
}
removedDroplets, err := RemoveAllDroplets(token)
if err != nil {
fmt.Fprintf(os.Stderr, "executing template: %s", err.Error())
}
sort.Strings(removedDroplets)

tmplData := struct {
RemovedDroplets []string
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/digitalocean/godo/.travis.yml

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

20 changes: 20 additions & 0 deletions vendor/github.com/digitalocean/godo/CHANGELOG.md

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

2 changes: 1 addition & 1 deletion vendor/github.com/digitalocean/godo/README.md

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

4 changes: 2 additions & 2 deletions vendor/github.com/digitalocean/godo/account.go

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

7 changes: 4 additions & 3 deletions vendor/github.com/digitalocean/godo/action.go

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

11 changes: 6 additions & 5 deletions vendor/github.com/digitalocean/godo/certificates.go

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

Loading

0 comments on commit e5abd63

Please sign in to comment.