Skip to content

Commit 0ea75bf

Browse files
committed
Enable explicitly specifying the client's public IP address (#7).
1 parent e7598ba commit 0ea75bf

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ Note that only OS images are supported for now, not customer images.
4343
Additionally, the OS must be a Linux distribution supported by docker-machine (Ubuntu 12.04 and above are supported, but RedHat 6 and 7 are not supported due to iptables configuration issues).
4444
* `ddcloud-ssh-user` - The SSH username to use.
4545
Default: "root".
46-
Environment: `MCP_SSH_USER`
46+
Environment: `MCP_SSH_USER`.
4747
* `ddcloud-ssh-key` - The SSH key file to use.
48-
Environment: `MCP_SSH_KEY`
48+
Environment: `MCP_SSH_KEY`.
4949
* `ddcloud-ssh-port` - The SSH port to use.
5050
Default: 22.
51-
Environment: `MCP_SSH_PORT`
51+
Environment: `MCP_SSH_PORT`.
5252
* `ddcloud-ssh-bootstrap-password` - The initial SSH password used to bootstrap SSH key authentication.
5353
This password is removed once the SSH key has been installed
5454
Environment: `MCP_SSH_BOOTSTRAP_PASSWORD`
5555
* `ddcloud-create-ssh-firewall-rule` - Automatically create a firewall rule to enable inbound SSH to the target server?
56+
* `ddcloud-client-public-ip` - Use the specified IPv4 address as the client's public IP address (don't auto-detect).
57+
Environment: `MCP_CLIENT_PUBLIC_IP`.
5658
* `ddcloud-use-private-ip` - Don't create NAT and firewall rules for target server (you will need to be connected to the VPN for your target data centre).
5759

5860
## Installing the provider

client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func (driver *Driver) isSSHFirewallRuleCreated() bool {
565565
}
566566

567567
// Create a firewall rule to enable inbound SSH connections to the target server from the client machine's (external) IP address.
568-
func (driver *Driver) createSSHFirewallRule(clientPublicIPAddress string) error {
568+
func (driver *Driver) createSSHFirewallRule() error {
569569
if !driver.isServerCreated() {
570570
return fmt.Errorf("Server '%s' has not been created", driver.MachineName)
571571
}
@@ -577,7 +577,7 @@ func (driver *Driver) createSSHFirewallRule(clientPublicIPAddress string) error
577577
log.Debugf("Creating SSH firewall rule for server '%s' (allow inbound traffic on port %d from '%s' to '%s')...",
578578
driver.MachineName,
579579
driver.SSHPort,
580-
clientPublicIPAddress,
580+
driver.ClientPublicIPAddress,
581581
driver.IPAddress,
582582
)
583583

@@ -589,7 +589,7 @@ func (driver *Driver) createSSHFirewallRule(clientPublicIPAddress string) error
589589
ruleConfiguration.Enable()
590590
ruleConfiguration.IPv4()
591591
ruleConfiguration.TCP()
592-
ruleConfiguration.MatchSourceAddress(clientPublicIPAddress)
592+
ruleConfiguration.MatchSourceAddress(driver.ClientPublicIPAddress)
593593
ruleConfiguration.MatchDestinationAddress(driver.IPAddress)
594594
ruleConfiguration.MatchDestinationPort(driver.SSHPort)
595595

client_public_ip.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package main
1010
import (
1111
"encoding/json"
1212
"fmt"
13+
"github.com/docker/machine/libmachine/log"
1314
"io/ioutil"
1415
"net/http"
1516
)
@@ -21,6 +22,8 @@ type ipInfo struct {
2122

2223
// Retrieve the client machine's public IPv4 address.
2324
func getClientPublicIPv4Address() (string, error) {
25+
log.Infof("Auto-detecting client's public (external) IP address...")
26+
2427
response, err := http.Get("https://ifconfig.co/json")
2528
if err != nil {
2629
return "", fmt.Errorf("Unable to connect to ifconfig.co to determine your IP address: %s", err.Error())

driver.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ type Driver struct {
8383
// The Id of the firewall rule (if any) created for inbound SSH access to the target server.
8484
SSHFirewallRuleID string
8585

86+
// The client's public (external) IP address.
87+
ClientPublicIPAddress string
88+
8689
// The CloudControl API client.
8790
client *compute.Client
8891
}
@@ -157,6 +160,12 @@ func (driver *Driver) GetCreateFlags() []mcnflag.Flag {
157160
Name: "ddcloud-create-ssh-firewall-rule",
158161
Usage: "Create a firewall rule to allow SSH access to the target server? Default: false",
159162
},
163+
mcnflag.StringFlag{
164+
EnvVar: "MCP_CLIENT_PUBLIC_IP",
165+
Name: "ddcloud-client-public-ip",
166+
Usage: "Use the specified IPv4 address as the client's public IP address (don't auto-detect)",
167+
Value: "",
168+
},
160169
mcnflag.BoolFlag{
161170
Name: "ddcloud-use-private-ip",
162171
Usage: "Don't create NAT and firewall rules for target server (you will need to be connected to the VPN for your target data centre). Default: false",
@@ -187,6 +196,7 @@ func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
187196
driver.SSHBootstrapPassword = flags.String("ddcloud-ssh-bootstrap-password")
188197

189198
driver.CreateSSHFirewallRule = flags.Bool("ddcloud-create-ssh-firewall-rule")
199+
driver.ClientPublicIPAddress = flags.String("ddcloud-client-public-ip")
190200
driver.UsePrivateIP = flags.Bool("ddcloud-use-private-ip")
191201

192202
log.Debugf("docker-machine-driver-ddcloud %s", DriverVersion)
@@ -266,21 +276,22 @@ func (driver *Driver) Create() error {
266276
log.Infof("Server '%s' has public IP '%s'.", driver.MachineName, driver.IPAddress)
267277

268278
if driver.CreateSSHFirewallRule {
269-
var clientPublicIPAddress string
270-
clientPublicIPAddress, err = getClientPublicIPv4Address()
271-
if err != nil {
272-
return err
279+
if driver.ClientPublicIPAddress == "" {
280+
driver.ClientPublicIPAddress, err = getClientPublicIPv4Address()
281+
if err != nil {
282+
return err
283+
}
273284
}
274285

275286
log.Infof("Creating firewall rule to enable inbound SSH traffic from local machine '%s' ('%s') to '%s' ('%s':%d)...",
276287
os.Getenv("HOST"),
277-
clientPublicIPAddress,
288+
driver.ClientPublicIPAddress,
278289
driver.MachineName,
279290
driver.IPAddress,
280291
driver.SSHPort,
281292
)
282293

283-
err = driver.createSSHFirewallRule(clientPublicIPAddress)
294+
err = driver.createSSHFirewallRule()
284295
if err != nil {
285296
return err
286297
}

0 commit comments

Comments
 (0)