junos-ez-srx 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ LICENSE (BSD-2)
2
+ ===============
3
+ Copyright (c) 2013, Jeremy Schulman, Juniper Networks
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in
14
+ the documentation and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # OVERVIEW
2
+
3
+ **NOTE: Work in progress - not yet in RubyGems.org**
4
+
5
+ A collection of Ruby classes to make Junos SRX automation Easy. This library supports the following
6
+ resources:
7
+
8
+ * Zones and associated interfaces
9
+ * Zone address-book entries
10
+ * Zone address-book sets
11
+ * Policy Rules
12
+ * Application entries
13
+ * Application sets
14
+
15
+ For more information about each topic, please refer to the **README_xyz.md** files.
16
+
17
+ # EXAMPLE USAGE
18
+
19
+ ````ruby
20
+ require 'pry'
21
+ require 'yaml'
22
+ require 'net/netconf/jnpr'
23
+ require 'junos-ez/stdlib'
24
+ require 'junos-ez/srx'
25
+
26
+ # login information for NETCONF session
27
+
28
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
29
+
30
+ ## create a NETCONF object to manage the device and open the connection ...
31
+
32
+ ndev = Netconf::SSH.new( login )
33
+ $stdout.print "Connecting to device #{login[:target]} ... "
34
+ ndev.open
35
+ $stdout.puts "OK!"
36
+
37
+ ## Now bind providers to the device object.
38
+ ## the 'Junos::Ez::Provider' must be first before all others
39
+ ## this provider will setup the device 'facts'. The other providers
40
+ ## allow you to define the instance variables; so this example
41
+ ## is using 'l1_ports' and 'ip_ports', but you could name them
42
+ ## what you like, yo!
43
+
44
+ Junos::Ez::Provider( ndev )
45
+ Junos::Ez::L1ports::Provider( ndev, :l1_ports )
46
+ Junos::Ez::IPports::Provider( ndev, :ip_ports )
47
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
48
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
49
+
50
+ ## drop into interactive mode to play around ... let's look
51
+ ## at what the device has for facts ...
52
+
53
+ #-> ndev.facts.list
54
+ #-> ndev.facts.catalog
55
+ #-> ndev.fact :version
56
+
57
+ ## now look at specific providers like the zones and policies
58
+
59
+ #-> ndev.zones.list
60
+ #-> ndev.zones.catalog
61
+
62
+ binding.pry
63
+
64
+ ndev.close
65
+ ````
66
+
67
+ # DEPENDENCIES
68
+
69
+ * gem netconf
70
+ * gem [junos-ez-stdlib](https://github.com/jeremyschulman/ruby-junos-ez-stdlib)
71
+
72
+
73
+ # INSTALLATION
74
+
75
+ * gem install junos-ez-srx (* JUST NOT YET *)
76
+
77
+ # CONTRIBUTORS
78
+
79
+ * Jeremy Schulman, @nwkautomaniac
80
+
81
+ # LICENSES
82
+
83
+ BSD-2, See LICENSE file
@@ -0,0 +1,25 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
7
+
8
+ ndev = Netconf::SSH.new( login )
9
+ ndev.open
10
+
11
+ Junos::Ez::Provider( ndev )
12
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
13
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
14
+ Junos::Ez::SRX::Apps::Provider( ndev, :apps )
15
+ Junos::Ez::SRX::AppSets::Provider( ndev, :appsets )
16
+
17
+ zone = ndev.zones["DEF-PROTECT-BZ-ST1"]
18
+ addr_set = zone.sets["SWITCHBOARD-MDM-UAT"]
19
+
20
+ app_list = ndev.apps.list
21
+ appset_list = ndev.appsets.list
22
+
23
+ binding.pry
24
+
25
+ ndev.close
data/examples/apps.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
7
+
8
+ ndev = Netconf::SSH.new( login )
9
+ ndev.open
10
+
11
+ Junos::Ez::Provider( ndev )
12
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
13
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
14
+ Junos::Ez::SRX::Apps::Provider( ndev, :apps )
15
+
16
+ a = ndev.apps["TCP-1024-3388"]
17
+
18
+ binding.pry
19
+
20
+ ndev.close
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
7
+
8
+ ndev = Netconf::SSH.new( login )
9
+ ndev.open
10
+
11
+ Junos::Ez::Provider( ndev )
12
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
13
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
14
+
15
+ policy = ndev.policies[ ["PII-SOX-BZ-ST1", "OUTSIDE-BZ-ST1"] ]
16
+
17
+ catalog_h = policy.rules.catalog_expanded
18
+ rule_530 = policy.rules.catalog_expanded( "530" )
19
+
20
+ ### find policy rules that have an application term with
21
+ ### a timeout of < 5 min == 300 seconds
22
+
23
+ t_5_m = catalog_h[:rules].select do |rule|
24
+ not( rule[:match_apps].select do |app_name, app_terms|
25
+ app_terms.select{|t| t[:timeout] > 0 and t[:timeout] < 300 }[0]
26
+ end.empty? )
27
+ end
28
+
29
+ binding.pry
30
+
31
+ ndev.close
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
7
+
8
+ ndev = Netconf::SSH.new( login )
9
+ ndev.open
10
+
11
+ Junos::Ez::Provider( ndev )
12
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
13
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
14
+
15
+ f_1 = ndev.zones.find_route "23.171.20.12", :addrs => true
16
+ f_2 = ndev.zones.find_route "23.171.37.37", :addrs => true
17
+
18
+ binding.pry
19
+
20
+ ndev.close
@@ -0,0 +1,78 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+
4
+ require 'junos-ez/stdlib'
5
+ require 'junos-ez/srx'
6
+
7
+ require_relative 'mylogins'
8
+
9
+ class JunosDevice < Netconf::SSH
10
+
11
+ # overload the open method to the Junos device and then
12
+ # create provider objects starting with Facts ...
13
+
14
+ def open
15
+ super # open connection to device
16
+ Junos::Ez::Facts::Provider( self ) # Facts must always be first!
17
+ Junos::Ez::Hosts::Provider( self, :hosts ) # manage staic host mapping
18
+ Junos::Ez::SysConfig::Provider( self, :syscfg )
19
+ Junos::Ez::StaticRoutes::Provider( self, :routes ) # manage static routes
20
+ Junos::Ez::L1ports::Provider( self, :l1_ports ) # manage IFD properties
21
+ Junos::Ez::IPports::Provider( self, :ip_ports ) # manage IPv4 interfaces
22
+ Junos::Ez::SRX::Zones::Provider( self, :zones ) # manage security zones
23
+ Junos::Ez::SRX::Policies::Provider( self, :zpols ) # manage secuirty policies
24
+ end
25
+
26
+ def rollback( rbid = 0 )
27
+ @rpc.load_configuration( :rollback => rbid.to_s )
28
+ end
29
+ def commit_check
30
+ @rpc.commit_configuration( :check => true )
31
+ end
32
+
33
+ end
34
+
35
+ target = ARGV[0] || "vsrx"
36
+ host = MyLogins::HOSTS[ target ]
37
+ filename = "srx-policy.yaml"
38
+
39
+ JunosDevice.new( host ) do |ndev|
40
+
41
+
42
+ from_zone_name = "PII-SOX-BZ-ST1"
43
+ to_zone_name = "OUTSIDE-BZ-ST1"
44
+
45
+ from_zone = ndev.zones[ from_zone_name ]
46
+ to_zone = ndev.zones[ to_zone_name ]
47
+ zpol_name = [ from_zone_name, to_zone_name ]
48
+ zpol = ndev.zpols[ zpol_name ]
49
+
50
+ binding.pry
51
+
52
+ ndev.zpols.create_from_yaml!( :filename=> filename, :replace=>true )
53
+
54
+ rule_list = zpol.rules.list!
55
+ rule = zpol.rules["545"]
56
+
57
+ # hash of new properties ...
58
+ new_rule_props = {
59
+ :description => "This is a test policy rule for JEREMY",
60
+ :match_srcs => ["S1","S2"],
61
+ :match_dsts => ["D1", "D2"],
62
+ :match_apps => ["any"],
63
+ :action => :permit
64
+ }
65
+
66
+ zpol.rules.create( "JEREMY", new_rule_props ) do |rule|
67
+ rule.write!
68
+ rule.reorder! :before => rule_list.last
69
+ end
70
+
71
+
72
+ end
73
+
74
+
75
+
76
+
77
+
78
+
@@ -0,0 +1,97 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ ###
7
+ ### load the data we want to use to represent the 'change request'
8
+ ###
9
+
10
+ change_data = YAML.load_file( 'change.yaml' )
11
+
12
+ ###
13
+ ### open a NETCONF connection to the target device
14
+ ###
15
+
16
+ login = {:target => 'vsrx', :username => 'jeremy', :password => 'jeremy1' }
17
+ ndev = Netconf::SSH.new( login )
18
+ ndev.open
19
+
20
+
21
+ ###
22
+ ### bind Junos EZ provider objects to the device object, you can pick the
23
+ ### instance variable names, I'm just using :zones and :policies, but
24
+ ### it's entirely up to you.
25
+ ###
26
+
27
+ Junos::Ez::Provider ndev
28
+ Junos::Ez::SRX::Zones::Provider ndev, :zones
29
+ Junos::Ez::SRX::Policies::Provider ndev, :policies
30
+
31
+ ###
32
+ ### select the provider resource objects for the from-zone and to-zone
33
+ ###
34
+
35
+ from_zone = ndev.zones[ change_data['from-zone']['name'] ]
36
+ to_zone = ndev.zones[ change_data['to-zone']['name'] ]
37
+
38
+ ### add the address names/sets to the proper zones.
39
+ ### if the address data is an Array, then we're adding
40
+ ### an address book set. otherwise we're adding an entry
41
+
42
+ {'from-zone' => from_zone, 'to-zone' => to_zone}.each do |which, zone|
43
+ change_data[which]['addresses'].each do |adr_name, adr_data|
44
+ case adr_data
45
+ when Array
46
+ set = zone.sets[adr_name]
47
+ set[:addr_names] = adr_data
48
+ set.write!
49
+ else
50
+ entry = zone.addrs[adr_name]
51
+ entry[:ip_prefix] = adr_data
52
+ entry.write!
53
+ end
54
+ end
55
+ end
56
+
57
+ ###
58
+ ### now add the new rule to the policy. insert the
59
+ ### new rule before the current last rule
60
+ ###
61
+
62
+ policy = ndev.policies[ [from_zone.name, to_zone.name] ]
63
+ last_rule = policy.rules.list.last
64
+
65
+ binding.pry
66
+
67
+ change_policy = change_data['policy']
68
+
69
+ rule = policy.rules[ change_policy['rule'] ]
70
+ rule[:action] = change_policy['action'].to_sym
71
+ rule[:match_srcs] = change_policy['from']
72
+ rule[:match_dsts] = change_policy['to']
73
+ rule[:match_apps] = change_policy['apps']
74
+ rule.write!
75
+ rule.reorder! :before => last_rule
76
+
77
+ ###
78
+ ### get a "diff" output of the changes and display them to the screen
79
+ ###
80
+
81
+ binding.pry
82
+
83
+ puts "Junos changes:\n"
84
+ config_diff = ndev.rpc.get_configuration(:compare=>'rollback', :rollback=>'0')
85
+ puts config_diff
86
+
87
+ # - breakpoint if we want to 'look around', just uncomment out the next line
88
+ # binding.pry
89
+
90
+ ###
91
+ ### now commit the configuration changes and close the connection
92
+ ###
93
+
94
+ ndev.rpc.commit_configuration
95
+ ndev.close
96
+
97
+
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ unless ARGV[0]
7
+ puts "You must specify a target"
8
+ end
9
+
10
+ login = { :target => ARGV[0], :username => 'jeremy', :password => 'jeremy1', }
11
+ ndev = Netconf::SSH.new( login )
12
+ ndev.open
13
+
14
+ Junos::Ez::Provider( ndev )
15
+ Junos::Ez::L1ports::Provider( ndev, :l1_ports )
16
+ Junos::Ez::IPports::Provider( ndev, :ip_ports )
17
+ Junos::Ez::SRX::Zones::Provider( ndev, :zones )
18
+ Junos::Ez::SRX::Policies::Provider( ndev, :policies )
19
+
20
+ binding.pry
21
+
22
+ ndev.close
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+ require 'junos-ez/srx'
5
+
6
+ def yaml_zone( zone )
7
+ zone.to_yaml :filename => zone.name + "_ZONE_IFS.yaml"
8
+ zone.addrs.to_yaml :filename => zone.name + "_ZONE_ADDRS.yaml"
9
+ zone.sets.to_yaml :filename => zone.name + "_ZONE_SETS.yaml"
10
+ end
11
+
12
+ login = {:target => 'vsrx', :username => 'jeremy', :password => 'jeremy1' }
13
+
14
+ ndev = Netconf::SSH.new( login )
15
+ ndev.open
16
+
17
+ Junos::Ez::Provider( ndev )
18
+ Junos::Ez::SRX::Zones.Provider( ndev, :zones )
19
+ Junos::Ez::SRX::Policies.Provider( ndev, :policies )
20
+
21
+ ### -----------------------------------------------------------------
22
+ ### dump all of the Zones to YAML
23
+ ### -----------------------------------------------------------------
24
+
25
+ ndev.zones.each do |zone|
26
+ next if zone.name == "junos-host"
27
+ $stdout.puts "Dumping Junos ZONE to YAML:[#{zone.name}]"
28
+ yaml_zone( zone )
29
+ end
30
+
31
+ ### -----------------------------------------------------------------
32
+ ### dump all of the Polcies to YAML
33
+ ### -----------------------------------------------------------------
34
+
35
+ ndev.policies.each do |policy|
36
+ from_zone, to_zone = policy.name
37
+ $stdout.puts "Dumping Junos POLICY to YAML: #{from_zone} --> #{to_zone}"
38
+ policy.to_yaml :filename => "POLICY__" + from_zone + "__" + to_zone + ".yaml"
39
+ end
40
+
41
+ ndev.close
42
+
43
+
44
+
45
+
46
+
47
+
48
+