View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.jboss.extension.resources.components;
17  
18  import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
19  
20  import javax.xml.stream.XMLStreamException;
21  
22  import org.jboss.as.controller.AttributeDefinition;
23  import org.jboss.as.controller.OperationFailedException;
24  import org.jboss.as.controller.OperationStepHandler;
25  import org.jboss.as.controller.SimpleAttributeDefinition;
26  import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
27  import org.jboss.as.controller.parsing.ParseUtils;
28  import org.jboss.as.controller.registry.AttributeAccess;
29  import org.jboss.as.controller.registry.ManagementResourceRegistration;
30  import org.jboss.dmr.ModelNode;
31  import org.jboss.dmr.ModelType;
32  import org.jboss.staxmapper.XMLExtendedStreamReader;
33  import org.jboss.staxmapper.XMLExtendedStreamWriter;
34  
35  import de.smartics.properties.api.core.security.SecurityConfiguration;
36  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
37  
38  /**
39   * Defines the security information of a configuration. This information allows
40   * to encrypt and decrypt property values.
41   * <p>
42   * Example XML fragment of a security element within a configuration:
43   * </p>
44   *
45   * <pre>
46   * {@markupExample "Security Element"
47   * &lt;security&gt;
48   *   &lt;algorithm&gt;AES&lt;/algorithm&gt;
49   *   &lt;transformation&gt;AES/ECB/PKCS5Padding&lt;/transformation&gt;
50   *   &lt;!-- provider / --&gt;
51   *   &lt;key&gt;+pvrmeQCmtWmYVOZ57uuIQ==&lt;/key&gt;
52   * &lt;/security&gt;}
53   * </pre>
54   */
55  public final class SecurityComponent extends AbstractComponent
56  {
57    // ********************************* Fields *********************************
58  
59    // --- constants ------------------------------------------------------------
60  
61    /**
62     * The name of the configuration XML element that holds the security
63     * information.
64     * <p>
65     * The value of this constant is {@value}.
66     * </p>
67     */
68    public static final String ROOT_ELEMENT_NAME = "security";
69  
70    /**
71     * The attribute that holds the security algorithm.
72     */
73    public static final SimpleAttributeDefinition ALGORITHM =
74        new SimpleAttributeDefinitionBuilder("security.algorithm",
75            ModelType.STRING, true).setXmlName("algorithm")
76            .setAllowExpression(true)
77            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
78  
79    /**
80     * The attribute that holds the security transformation.
81     */
82    public static final SimpleAttributeDefinition TRANSFORMATION =
83        new SimpleAttributeDefinitionBuilder("security.transformation",
84            ModelType.STRING, true).setXmlName("transformation")
85            .setAllowExpression(true)
86            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
87  
88    /**
89     * The attribute that holds the security provider.
90     */
91    public static final SimpleAttributeDefinition PROVIDER =
92        new SimpleAttributeDefinitionBuilder("security.provider",
93            ModelType.STRING, true).setXmlName("provider")
94            .setAllowExpression(true)
95            .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
96  
97    /**
98     * The attribute that holds the security key.
99     */
100   public static final SimpleAttributeDefinition KEY =
101       new SimpleAttributeDefinitionBuilder("security.key", ModelType.STRING,
102           true).setXmlName("key").setAllowExpression(true)
103           .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
104 
105   private static final AttributeDefinition[] DEFINITIONS = { ALGORITHM,
106                                                             TRANSFORMATION,
107                                                             PROVIDER, KEY };
108 
109   // --- members --------------------------------------------------------------
110 
111   // ****************************** Initializer *******************************
112 
113   // ****************************** Constructors ******************************
114 
115   /**
116    * Default constructor.
117    */
118   public SecurityComponent(final OperationStepHandler writeHandler)
119   {
120     super(writeHandler);
121   }
122 
123   // ****************************** Inner Classes *****************************
124 
125   // ********************************* Methods ********************************
126 
127   // --- init -----------------------------------------------------------------
128 
129   // --- get&set --------------------------------------------------------------
130 
131   // --- business -------------------------------------------------------------
132 
133   public void registerAttributes(
134       final ManagementResourceRegistration resourceRegistration)
135   {
136     resourceRegistration.registerReadWriteAttribute(ALGORITHM, null,
137         writeHandler);
138     resourceRegistration.registerReadWriteAttribute(TRANSFORMATION, null,
139         writeHandler);
140     resourceRegistration.registerReadWriteAttribute(PROVIDER, null,
141         writeHandler);
142     resourceRegistration.registerReadWriteAttribute(KEY, null, writeHandler);
143   }
144 
145   public void populateModel(final ModelNode operation, final ModelNode model)
146     throws OperationFailedException
147   {
148     ALGORITHM.validateAndSet(operation, model);
149     TRANSFORMATION.validateAndSet(operation, model);
150     PROVIDER.validateAndSet(operation, model);
151     KEY.validateAndSet(operation, model);
152   }
153 
154   public void read(final XMLExtendedStreamReader reader,
155       final ModelNode addOperation) throws XMLStreamException
156   {
157     ParseUtils.requireNoAttributes(reader);
158 
159     while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
160     {
161       if (reader.isStartElement())
162       {
163         final String localName = reader.getLocalName();
164 
165         if (SecurityComponent.ALGORITHM.getXmlName().equals(localName))
166         {
167           readAttribute(reader, addOperation, SecurityComponent.ALGORITHM);
168         }
169         else if (SecurityComponent.TRANSFORMATION.getXmlName()
170             .equals(localName))
171         {
172           readAttribute(reader, addOperation, SecurityComponent.TRANSFORMATION);
173         }
174         else if (SecurityComponent.PROVIDER.getXmlName().equals(localName))
175         {
176           readAttribute(reader, addOperation, SecurityComponent.PROVIDER);
177         }
178         else if (SecurityComponent.KEY.getXmlName().equals(localName))
179         {
180           readAttribute(reader, addOperation, SecurityComponent.KEY);
181         }
182         else
183         {
184           throw ParseUtils.unexpectedElement(reader);
185         }
186       }
187     }
188   }
189 
190   public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
191     throws XMLStreamException
192   {
193     if (isAtLeastOneDefined(config, DEFINITIONS))
194     {
195       writer.writeStartElement(SecurityComponent.ROOT_ELEMENT_NAME);
196 
197       for (final AttributeDefinition definition : DEFINITIONS)
198       {
199         writeAttribute(writer, config, definition);
200       }
201 
202       writer.writeEndElement();
203     }
204   }
205 
206   public void apply(final PropertiesConfiguration propertiesConfig,
207       final ModelNode model)
208   {
209     final SecurityConfiguration securityConfig =
210         propertiesConfig.getSecurityConfiguration();
211 
212     final String algorithm = getAttribute(model, ALGORITHM);
213     securityConfig.setAlgorithm(algorithm);
214 
215     final String transformation = getAttribute(model, TRANSFORMATION);
216     securityConfig.setTransformation(transformation);
217 
218     final String provider = getAttribute(model, PROVIDER);
219     securityConfig.setProvider(provider);
220 
221     final String key = getAttribute(model, KEY);
222     securityConfig.setKey(key);
223   }
224 
225   // --- object basics --------------------------------------------------------
226 
227 }