File: PolicyContext.java

package info (click to toggle)
jetty8 8.1.16-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,248 kB
  • ctags: 24,086
  • sloc: java: 202,954; xml: 10,804; sh: 1,125; jsp: 250; makefile: 49; sql: 35
file content (200 lines) | stat: -rw-r--r-- 5,604 bytes parent folder | download | duplicates (3)
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
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.policy;

import java.io.File;
import java.security.KeyStore;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

public class PolicyContext
{
    private Map<String, String> properties = new HashMap<String, String>();
    
    private Principal[] principals;
    private KeyStore keystore;
    
    public PolicyContext()
    {
        // special property case for resolving ${/} to native separator
        properties.put( "/", File.separator );
    }
    
    public void addProperty( String name, String value )
    {
        this.properties.put( name, value );
    }
    
    public void setProperties( Map<String,String> properties )
    {
        this.properties.putAll( properties );
    }

    public KeyStore getKeystore()
    {
        return keystore;
    }

    public void setKeystore( KeyStore keystore )
    {
        this.keystore = keystore;
    }  

    public Principal[] getPrincipals()
    {
        return principals;
    }

    public void setPrincipals( Principal[] principals )
    {
        this.principals = principals;
    }

    public String evaluate(String s) throws PolicyException
    {       
        s = processProtocols( s );
        
        int i1=0;
        int i2=0;

        while (s!=null)
        {
            i1=s.indexOf("${");
            if (i1<0)
            {
                break;
            }
            
            i2=s.indexOf("}",i1+2);
            if (i2<0)
            {
                break;
            }
     
            String property=getProperty(s.substring(i1+2,i2));
       
            s=s.substring(0,i1)+property+s.substring(i2+1);         
        }
        
        return s;
    }
    
    private String processProtocols( String s ) throws PolicyException
    {
        int i1=0;
        int i2=0;

        while (s!=null)
        {
            i1=s.indexOf("${{");
            if (i1<0)
            {
                break;
            }
            
            i2=s.indexOf("}}",i1+2);
            if (i2<0)
            {
                break;
            }
     
            String property;
            String target = s.substring(i1+3,i2);
            
            if ( target.indexOf( ":" ) >= 0 )
            {
                String[] resolve = target.split( ":" );
                property = resolve(resolve[0], resolve[1] );
            }
            else
            {
                property = resolve( target, null );
            }
            s=s.substring(0,i1)+property+s.substring(i2+2);
        }
        
        return s;
    }
    
    
    public String getProperty(String name)
    {       
        if (properties.containsKey(name))
        {
            return properties.get(name);
        }
        
        return System.getProperty(name);
    }
    
    private String resolve( String protocol, String data ) throws PolicyException
    {

        if ( "self".equals( protocol ) ) 
        { 
            // need expanding to list of principals in grant clause
            if ( principals != null && principals.length != 0 )
            {
                StringBuilder sb = new StringBuilder();
                for ( int i = 0; i < principals.length; ++i )
                {
                    sb.append( principals[i].getClass().getName() );
                    sb.append( " \"" );
                    sb.append( principals[i].getName() );
                    sb.append( "\" " );
                }
                return sb.toString();
            }
            else
            {
                throw new PolicyException( "self can not be expanded, missing principals" );
            }
        }
        if ( "alias".equals( protocol ) ) 
        { 
            try
            {
                 Certificate cert = keystore.getCertificate(data);
               
                 if ( cert instanceof X509Certificate )
                 {
                     Principal principal = ((X509Certificate) cert).getSubjectX500Principal(); 
                     StringBuilder sb = new StringBuilder();
                     sb.append( principal.getClass().getName() );
                     sb.append( " \"" );
                     sb.append( principal.getName() );
                     sb.append( "\" " );
                     return sb.toString();
                 }
                 else
                 {
                     throw new PolicyException( "alias can not be expanded, bad cert" );
                 }
            }
            catch ( Exception e )
            {
                throw new PolicyException( "alias can not be expanded: " + data );
            }
        }
        throw new PolicyException( "unknown protocol: " + protocol );
    }    
}