Skip to content

Commit 33a2bbb

Browse files
committed
Merge branch 'release/v0.4.0'
2 parents e7103fe + 6664c2a commit 33a2bbb

12 files changed

+222
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ test/version_tmp
1818
tmp
1919
.ruby-version
2020
.ruby-gemset
21+
.ovpnmcgen.rb.yml

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ before_install:
55
rvm:
66
- 1.9.3
77
- 2.0.0
8-
- 2.1-head
8+
- 2.1
99
- ruby-head
1010
- jruby-19mode
1111

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
= 0.4.0 / 2014-05-04
2+
* VoD rules in `--[un]trusted-ssids` to also use `InterfaceTypeMatch`.
3+
* Added support for configuration persistance, via ENV or ~/.ovpnmcgen.rb.yml or `--config` flag.
4+
15
= 0.3.0 / 2014-05-04
26
* Documentation updates.
37
* Added support for `URLStringProbe`, via `--url-probe`.

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Build and install the gem:
4444
Usage: ovpnmcgen.rb generate [options] <user> <device>
4545
4646
Options:
47+
-c, --config FILE Specify path to config file. [Default: .ovpnmcgen.rb.yml]
4748
--cafile FILE Path to OpenVPN CA file. (Required)
4849
--tafile FILE Path to TLS-Auth Key file.
4950
--host HOSTNAME Hostname of OpenVPN server. (Required)
@@ -63,6 +64,12 @@ Usage: ovpnmcgen.rb generate [options] <user> <device>
6364
-o, --output FILE Output to file. [Default: stdout]
6465
```
6566

67+
### Configuration
68+
69+
Option flags can be set using environment variables or placed into a YAML formatted file. The default filename `.ovpnmcgen.rb.yml` will be searched for in `./`, and then `~/`.
70+
71+
Note: Only for YAML configuration files and environment variables, flags with hyphens (-) are replaced with underscores (_), i.e. `--trusted-ssids safe` should be `trusted_ssids: safe`.
72+
6673
### Security Levels
6774

6875
There are three different security levels to choose from, 'paranoid', 'high' (default), and 'medium'. The algorithm illustrated above is for 'high'.

bin/ovpnmcgen.rb

+41-18
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
require 'ovpnmcgen'
44
require 'commander/import'
5+
require 'ovpnmcgen/config'
56

67
program :version, Ovpnmcgen::VERSION
78
program :description, Ovpnmcgen::SUMMARY
89
program :help, 'Usage', 'ovpnmcgen.rb <command> [options] <args...>'
910
program :help_formatter, :compact
1011
default_command :help
1112
never_trace!
12-
#global_option '-c', '--config FILE', 'Specify path to config file' #not implemented yet
13+
global_option '-c', '--config FILE', 'Specify path to config file. [Default: .ovpnmcgen.rb.yml]'
1314

1415
command :generate do |c|
1516
c.syntax = 'ovpnmcgen.rb generate [options] <user> <device>'
@@ -37,31 +38,53 @@
3738
c.option '-o', '--output FILE', 'Output to file. [Default: stdout]'
3839
c.action do |args, options|
3940
raise ArgumentError.new "Invalid arguments. Run '#{File.basename(__FILE__)} help generate' for guidance" if args.nil? or args.length < 2
40-
raise ArgumentError.new "Host is required" unless options.host
41-
raise ArgumentError.new "cafile is required" unless options.cafile
42-
raise ArgumentError.new "PKCS#12 file is required" unless options.p12file
43-
options.default :vod => true, :proto => 'udp', :port => 1194, :security_level => 'high'
44-
user, device, p12file, p12pass = args
41+
42+
# Set up configuration environment.
43+
if options.config
44+
Ovpnmcgen.configure(options.config)
45+
else
46+
Ovpnmcgen.configure
47+
end
48+
config = Ovpnmcgen.config
49+
50+
raise ArgumentError.new "Host is required" unless options.host or config.host
51+
raise ArgumentError.new "cafile is required" unless options.cafile or config.cafile
52+
raise ArgumentError.new "PKCS#12 file is required" unless options.p12file or config.p12file
53+
54+
options.default :vod => case
55+
when config.vod == true || config.no_vod == false
56+
true
57+
when config.vod == false || config.no_vod == true
58+
false
59+
else # enabled by default
60+
true
61+
end,
62+
:proto => (config.proto)? config.proto : 'udp',
63+
:port => (config.port)? config.port : 1194,
64+
:security_level => (config.security_level)? config.security_level : 'high'
65+
66+
user, device = args
67+
4568
inputs = {
4669
:user => user,
4770
:device => device,
48-
:p12file => options.p12file,
49-
:p12pass => options.p12pass,
50-
:cafile => options.cafile,
51-
:host => options.host,
71+
:p12file => options.p12file || config.p12file,
72+
:p12pass => options.p12pass || config.p12pass,
73+
:cafile => options.cafile || config.cafile,
74+
:host => options.host || config.host,
5275
:proto => options.proto,
5376
:port => options.port,
5477
:enableVOD => options.vod,
55-
:trusted_ssids => options.trusted_ssids,
56-
:untrusted_ssids => options.untrusted_ssids,
57-
:profile_uuid => options.profile_uuid,
58-
:vpn_uuid => options.vpn_uuid,
59-
:cert_uuid => options.cert_uuid,
78+
:trusted_ssids => options.trusted_ssids || config.trusted_ssids,
79+
:untrusted_ssids => options.untrusted_ssids || config.untrusted_ssids,
80+
:profile_uuid => options.profile_uuid || config.profile_uuid,
81+
:vpn_uuid => options.vpn_uuid || config.vpn_uuid,
82+
:cert_uuid => options.cert_uuid || config.cert_uuid,
6083
:security_level => options.security_level
6184
}
62-
inputs[:ovpnconfigfile] = options.ovpnconfigfile if options.ovpnconfigfile
63-
inputs[:tafile] = options.tafile if options.tafile
64-
inputs[:url_probe] = options.url_probe if options.url_probe
85+
inputs[:ovpnconfigfile] = options.ovpnconfigfile || config.ovpnconfigfile if options.ovpnconfigfile or config.ovpnconfigfile
86+
inputs[:tafile] = options.tafile || config.tafile if options.tafile or config.tafile
87+
inputs[:url_probe] = options.url_probe || config.url_probe if options.url_probe or config.url_probe
6588

6689
unless options.output
6790
puts Ovpnmcgen.generate(inputs)

features/gen_basic.feature

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ Feature: Basic Generate Functionality
156156
Then the output should match:
157157
"""
158158
<string>Disconnect</string>
159+
\s*<key>InterfaceTypeMatch</key>
160+
\s*<string>WiFi</string>
159161
\s*<key>SSIDMatch</key>
160162
\s*<array>
161163
\s*<string>trusted1</string>
@@ -165,6 +167,8 @@ Feature: Basic Generate Functionality
165167
And the output should match:
166168
"""
167169
<string>Connect</string>
170+
\s*<key>InterfaceTypeMatch</key>
171+
\s*<string>WiFi</string>
168172
\s*<key>SSIDMatch</key>
169173
\s*<array>
170174
\s*<string>evil3</string>

features/gen_configfile.feature

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Feature: Generate Functionality with Configuration File
2+
In order to generate a properly formatted plist mobileconfig with less typing
3+
As a CLI
4+
Some basic inputs are taken from a config file, if available
5+
6+
Background:
7+
Given a file named "ca.crt" with:
8+
"""
9+
Contents of CA file
10+
With newlines
11+
And more newlines
12+
That should appear as one line
13+
"""
14+
And a file named "p12file.p12" with:
15+
"""
16+
p12file that should appear
17+
In base64 encoding as <data/>
18+
"""
19+
20+
Scenario: A configuration file supplied should be read, without the need for required flags.
21+
Given a file named ".ovpnmcgen.rb.yml" with:
22+
"""
23+
host: aruba.cucumber.org
24+
"""
25+
When I run `ovpnmcgen.rb g cucumber aruba`
26+
Then the output should contain "error: "
27+
And the output should not contain "error: Host"
28+
29+
Scenario: A custom configuration file supplied should be read, without the need for required flags.
30+
Given a file named ".custom.yml" with:
31+
"""
32+
host: aruba.cucumber.org
33+
"""
34+
When I run `ovpnmcgen.rb g --config .custom.yml cucumber aruba`
35+
Then the output should contain "error: "
36+
And the output should not contain "error: Host"
37+
38+
Scenario: Flags should override configuration file options.
39+
Given a file named ".ovpnmcgen.rb.yml" with:
40+
"""
41+
host: file.org
42+
no_vod: true
43+
"""
44+
When I run `ovpnmcgen.rb g --host aruba.cucumber.org --cafile ca.crt --vod --p12file p12file.p12 cucumber aruba`
45+
Then the output should match:
46+
"""
47+
<key>remote</key>
48+
\s*<string>aruba.cucumber.org 1194 udp</string>
49+
"""
50+
And the output should match:
51+
"""
52+
<key>OnDemandEnabled</key>
53+
\s*<integer>1</integer>
54+
"""
55+
And the output should not match:
56+
"""
57+
<key>remote</key>
58+
\s*<string>file.org 1194 udp</string>
59+
"""
60+
61+
Scenario: Battle between no-vod in the configuration file and the vod flag default.
62+
Given a file named ".ovpnmcgen.rb.yml" with:
63+
"""
64+
no_vod: false
65+
"""
66+
When I run `ovpnmcgen.rb g --host aruba.cucumber.org --cafile ca.crt --p12file p12file.p12 cucumber aruba`
67+
Then the output should match:
68+
"""
69+
<key>OnDemandEnabled</key>
70+
\s*<integer>1</integer>
71+
"""
72+
73+
Scenario: no_vod true in the configuration file.
74+
Given a file named ".ovpnmcgen.rb.yml" with:
75+
"""
76+
no_vod: true
77+
"""
78+
When I run `ovpnmcgen.rb g --host aruba.cucumber.org --cafile ca.crt --p12file p12file.p12 cucumber aruba`
79+
Then the output should match:
80+
"""
81+
<key>OnDemandEnabled</key>
82+
\s*<integer>0</integer>
83+
"""
84+
85+
Scenario: ENV variables set here should work.
86+
Given I set the environment variable "OG_HOST" to "env.org"
87+
When I run `/usr/bin/env`
88+
Then the output should contain "OG_HOST=env.org"
89+
90+
Scenario: ENV variables should override configuration file options.
91+
Given a file named ".ovpnmcgen.rb.yml" with:
92+
"""
93+
host: file.org
94+
"""
95+
And I set the environment variable "OG_HOST" to "env.org"
96+
When I run `ovpnmcgen.rb g --cafile ca.crt --p12file p12file.p12 cucumber aruba`
97+
Then the output should match:
98+
"""
99+
<key>remote</key>
100+
\s*<string>env.org 1194 udp</string>
101+
"""
102+
And the output should not match:
103+
"""
104+
<key>remote</key>
105+
\s*<string>file.org 1194 udp</string>
106+
"""
107+
108+
Scenario: Flags should overrride ENV variables, and should also override configuration file options.
109+
Given a file named ".ovpnmcgen.rb.yml" with:
110+
"""
111+
host: file.org
112+
"""
113+
And I set the environment variable "OG_HOST" to "env.org"
114+
When I run `ovpnmcgen.rb g --host aruba.cucumber.org --cafile ca.crt --p12file p12file.p12 cucumber aruba`
115+
Then the output should match:
116+
"""
117+
<key>remote</key>
118+
\s*<string>aruba.cucumber.org 1194 udp</string>
119+
"""
120+
And the output should not match:
121+
"""
122+
<key>remote</key>
123+
\s*<string>env.org 1194 udp</string>
124+
"""
125+
And the output should not match:
126+
"""
127+
<key>remote</key>
128+
\s*<string>file.org 1194 udp</string>
129+
"""

features/step_definitions/env.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Given /^I set the environment variable "(\w+)" to "([^"]*)"$/ do |var, value|
2+
ENV[var] = value
3+
end

lib/ovpnmcgen.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ def generate(inputs = {})
5656

5757
vpnOnDemandRules = Array.new
5858
vodTrusted = { # Trust only Wifi SSID
59+
'InterfaceTypeMatch' => 'WiFi',
5960
'SSIDMatch' => trusted_ssids,
6061
'Action' => 'Disconnect'
6162
}
6263
vodUntrusted = { # Untrust Wifi
64+
'InterfaceTypeMatch' => 'WiFi',
6365
'SSIDMatch' => untrusted_ssids,
6466
'Action' => 'Connect'
6567
}
@@ -88,7 +90,12 @@ def generate(inputs = {})
8890
}
8991

9092
# Insert URLStringProbe conditions when enabled with --url-probe
91-
vodTrusted['URLStringProbe'] = vodUntrusted['URLStringProbe'] = vodWifiOnly['URLStringProbe'] = vodCellularOnly['URLStringProbe'] = vodDefault['URLStringProbe'] = inputs[:url_probe] if inputs[:url_probe]
93+
vodTrusted['URLStringProbe'] =
94+
vodUntrusted['URLStringProbe'] =
95+
vodWifiOnly['URLStringProbe'] =
96+
vodCellularOnly['URLStringProbe'] =
97+
vodDefault['URLStringProbe'] =
98+
inputs[:url_probe] if inputs[:url_probe]
9299

93100
vpnOnDemandRules << vodTrusted if trusted_ssids
94101
vpnOnDemandRules << vodUntrusted if untrusted_ssids

lib/ovpnmcgen/config.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'app_configuration'
2+
3+
module Ovpnmcgen
4+
@@config_file_name = '.ovpnmcgen.rb.yml'
5+
6+
# attr_accessor :config, :config_file_name
7+
8+
def configure(filename = @@config_file_name)
9+
10+
@@config = AppConfiguration.new filename do
11+
prefix 'og'
12+
end
13+
14+
# @@config = AppConfiguration[:ovpnmcgen]
15+
end
16+
17+
def config
18+
@@config
19+
end
20+
21+
module_function :configure, :config
22+
end

lib/ovpnmcgen/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module Ovpnmcgen
2-
VERSION = "0.3.0"
2+
VERSION = "0.4.0"
33
SUMMARY = "An OpenVPN iOS Configuration Profile (.mobileconfig) Utility"
44
end

ovpnmcgen.rb.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "aruba", "~> 0.5", ">= 0.5.4"
2626
spec.add_runtime_dependency "plist", "~> 3.1", ">= 3.1.0"
2727
spec.add_runtime_dependency "commander", "~> 4.1", ">= 4.1.6"
28+
spec.add_runtime_dependency "app_configuration", "~> 0.0", ">= 0.0.2"
2829
end

0 commit comments

Comments
 (0)