File tree Expand file tree Collapse file tree 2 files changed +85
-1
lines changed Expand file tree Collapse file tree 2 files changed +85
-1
lines changed Original file line number Diff line number Diff line change 1
1
module OSLResources
2
2
module Cookbook
3
3
module Helpers
4
+ require 'ipaddr'
5
+ require 'iniparse'
6
+
4
7
# osl_ifconfig helpers
5
8
def default_nm_controlled
6
9
node [ 'platform_version' ] . to_i >= 8 ? 'yes' : 'no'
7
10
end
8
11
9
12
# Based on https://github.com/chef/chef/blob/61a8aa44ac33fc3bbeb21fa33acf919a97272eb7/lib/chef/resource/systemd_unit.rb#L66-L83
10
13
def to_ini ( content )
11
- require 'iniparse'
12
14
case content
13
15
when Hash
14
16
IniParse . gen do |doc |
@@ -50,8 +52,58 @@ def virtualbox_packages
50
52
]
51
53
end
52
54
end
55
+
56
+ def osl_local_ipv4?
57
+ local = false
58
+ ip = IPAddr . new ( node [ 'ipaddress' ] )
59
+ osl_local_ip . each do |net |
60
+ net = IPAddr . new net
61
+ local = net . include? ( ip )
62
+ break if local
63
+ end
64
+ local
65
+ end
66
+
67
+ def osl_local_ipv6?
68
+ # If we don't have an IPv6, let's just assume it's false
69
+ return false unless node [ 'ip6address' ]
70
+
71
+ local = false
72
+ ip = IPAddr . new ( node [ 'ip6address' ] )
73
+ osl_local_ip . each do |net |
74
+ net = IPAddr . new net
75
+ local = net . include? ( ip )
76
+ break if local
77
+ end
78
+ local
79
+ end
80
+
81
+ private
82
+
83
+ def osl_local_ip
84
+ # These are local to the OSU campus
85
+ [
86
+ '10.0.0.0/23' ,
87
+ '10.1.0.0/23' ,
88
+ '10.1.2.0/23' ,
89
+ '10.1.100.0/22' ,
90
+ '10.6.4.0/22' ,
91
+ '10.162.136.0/24' , # Milne Workstation subnet
92
+ '128.193.126.192/28' , # Milne Server subnet
93
+ '128.193.152.128/27' , # OSU Gateway from Milne workstations
94
+ '140.211.9.0/24' ,
95
+ '140.211.10.0/24' ,
96
+ '140.211.15.0/24' ,
97
+ '140.211.166.0/23' ,
98
+ '140.211.168.0/24' ,
99
+ '140.211.169.0/24' ,
100
+ '2605:bc80:3010::/48' ,
101
+ ]
102
+ end
53
103
end
54
104
end
55
105
end
56
106
Chef ::DSL ::Recipe . include ::OSLResources ::Cookbook ::Helpers
57
107
Chef ::Resource . include ::OSLResources ::Cookbook ::Helpers
108
+ # Needed to used in attributes/
109
+ Chef ::Node . include ::OSLResources ::Cookbook ::Helpers
Original file line number Diff line number Diff line change
1
+ require_relative '../../spec_helper'
2
+ require_relative '../../../libraries/helpers'
3
+
4
+ RSpec . describe OSLResources ::Cookbook ::Helpers do
5
+ class DummyClass < Chef ::Node
6
+ include OSLResources ::Cookbook ::Helpers
7
+ end
8
+
9
+ subject { DummyClass . new }
10
+
11
+ describe '#osl_local_ipv4?' do
12
+ it 'local IPv4 address' do
13
+ allow ( subject ) . to receive ( :[] ) . with ( 'ipaddress' ) . and_return ( '140.211.166.130' )
14
+ expect ( subject . osl_local_ipv4? ) . to eq true
15
+ end
16
+ it 'external IPv4 address' do
17
+ allow ( subject ) . to receive ( :[] ) . with ( 'ipaddress' ) . and_return ( '216.165.191.54' )
18
+ expect ( subject . osl_local_ipv4? ) . to eq false
19
+ end
20
+ end
21
+
22
+ describe '#osl_local_ipv6?' do
23
+ it 'local IPv6 address' do
24
+ allow ( subject ) . to receive ( :[] ) . with ( 'ip6address' ) . and_return ( '2605:bc80:3010::130' )
25
+ expect ( subject . osl_local_ipv6? ) . to eq true
26
+ end
27
+ it 'external IPv6 address' do
28
+ allow ( subject ) . to receive ( :[] ) . with ( 'ip6address' ) . and_return ( '2600:3402:600:24::154' )
29
+ expect ( subject . osl_local_ipv6? ) . to eq false
30
+ end
31
+ end
32
+ end
You can’t perform that action at this time.
0 commit comments