summaryrefslogtreecommitdiff
path: root/trunk/qpid/java/systests/src/main/java/org/apache/qpid/server/security/firewall/FirewallConfigTest.java
blob: cbdb310179da4be65c7fde2bfebe1aea29e9f96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.apache.qpid.server.security.firewall;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.jms.Connection;
import javax.jms.JMSException;

import org.apache.qpid.client.AMQConnectionURL;
import org.apache.qpid.test.utils.QpidTestCase;

public class FirewallConfigTest extends QpidTestCase 
{

    private File tmpFile = null;
    @Override
    protected void setUp() throws Exception
    {
        // do setup
        final String QPID_HOME = System.getProperty("QPID_HOME");

        if (QPID_HOME == null)
        {
            fail("QPID_HOME not set");
        }

        // Setup initial config.
        _configFile = new File(QPID_HOME, "etc/config-systests-firewall.xml");
        tmpFile = File.createTempFile("config-systests-firewall", ".xml");
        setSystemProperty("QPID_FIREWALL_SETTINGS", tmpFile.getAbsolutePath());
        tmpFile.deleteOnExit();
    }

    private void writeFirewallFile(boolean allow, boolean inVhost) throws IOException
    {
        FileWriter out = new FileWriter(tmpFile);
        String ipAddr = "127.0.0.1"; // FIXME: get this from InetAddress.getLocalHost().getAddress() ?
        out.write("<broker>");
        if (inVhost) 
        {
            out.write("<virtualhosts><virtualhost><test>");
        }
        out.write("<security><firewall>");
        out.write("<rule access=\""+((allow) ? "allow" : "deny")+"\" network=\""+ipAddr +"\"/>");
        out.write("</firewall></security>");
        if (inVhost)
        {
            out.write("</test></virtualhost></virtualhosts>");
        }
        out.write("</broker>");
        out.close();
    }
    
    public void testVhostAllowBrokerDeny() throws Exception
    {
        if (_broker.equals(VM))
        {
            //No point running this test with an InVM broker as the
            //firewall plugin only functions for TCP connections.
            return;
        }

        _configFile = new File(System.getProperty("QPID_HOME"), "etc/config-systests-firewall-2.xml");
        
        super.setUp();
        
        Connection conn = null;
        try 
        {
            //Try to get a connection to the 'test2' vhost
            //This is expected to fail as it is denied at the broker level
            conn = getConnection(new AMQConnectionURL(
                    "amqp://username:password@clientid/test2?brokerlist='" + getBroker() + "'"));
            fail("We expected the connection to fail");
        } 
        catch (JMSException e)
        {
            //ignore
        }
        
        conn = null;
        try 
        {
            //Try to get a connection to the 'test' vhost
            //This is expected to succeed as it is allowed at the vhost level
            conn = getConnection();
        } 
        catch (JMSException e)
        {
            e.getLinkedException().printStackTrace();
            fail("The connection was expected to succeed: " + e.getMessage());
        }
    }
    
    public void testVhostDenyBrokerAllow() throws Exception
    {
        if (_broker.equals(VM))
        {
            //No point running this test with an InVM broker as the
            //firewall plugin only functions for TCP connections.
            return;
        }
        
        _configFile = new File(System.getProperty("QPID_HOME"), "etc/config-systests-firewall-3.xml");
        
        super.setUp();
        
        Connection conn = null;
        try 
        {
            //Try to get a connection to the 'test2' vhost
            //This is expected to fail as it is denied at the vhost level
            conn = getConnection(new AMQConnectionURL(
                    "amqp://username:password@clientid/test2?brokerlist='" + getBroker() + "'"));
        } 
        catch (JMSException e)
        {
            //ignore
        }

        conn = null;
        try 
        {
            //Try to get a connection to the 'test' vhost
            //This is expected to succeed as it is allowed at the broker level
            conn = getConnection();
        } 
        catch (JMSException e)
        {
            e.getLinkedException().printStackTrace();
            fail("The connection was expected to succeed: " + e.getMessage());
        }
    }
 
    public void testDenyOnRestart() throws Exception
    {
        testDeny(false, new Runnable() {

            public void run()
            {
                try
                {
                    restartBroker();
                } catch (Exception e)
                {
                    fail(e.getMessage());
                }
            }
        });
    }
    
    public void testDenyOnRestartInVhost() throws Exception
    {
        testDeny(true, new Runnable() {

            public void run()
            {
                try
                {
                    restartBroker();
                } catch (Exception e)
                {
                    fail(e.getMessage());
                }
            }
        });
    }
    
    public void testDenyOnReload() throws Exception
    {
        testDeny(false, new Runnable() {

            public void run()
            {
                try
                {
                    reloadBroker();
                } catch (Exception e)
                {
                    fail(e.getMessage());
                }
            }
        }
        );
    }
    
    public void testDenyOnReloadInVhost() throws Exception
    {
        testDeny(true, new Runnable() {

            public void run()
            {
                try
                {
                    reloadBroker();
                } catch (Exception e)
                {
                   fail(e.getMessage());
                }
            }
        }
        );
       
    }
    
    private void testDeny(boolean inVhost, Runnable restartOrReload) throws Exception
    {
        if (_broker.equals(VM))
        {
            // No point running this test in a vm broker
            return;
        }
        
        writeFirewallFile(false, inVhost);        
        super.setUp();
        
        Exception exception  = null;
        Connection conn = null;
        try 
        {
            conn = getConnection();
        } 
        catch (JMSException e)
        {
            exception = e;
        }
        assertNotNull(exception);
        
        // Check we can get a connection

        writeFirewallFile(true, inVhost);
        restartOrReload.run();
        
        exception = null;
        try 
        {
            conn = getConnection();
        } 
        catch (JMSException e)
        {
            exception = e;
        }
        assertNull(exception);
    }    
}