View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015-2016 ForgeRock AS.
15   */
16  package org.forgerock.audit.handlers.csv;
17  
18  import java.io.IOException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.io.ICsvMapReader;
26  
27  /**
28   * This class to read a secure CSV file. It completely hides the last column, which is the HMAC column.
29   * It does not do any checking regarding the HMAC validity.
30   *
31   */
32  class CsvSecureMapReader implements ICsvMapReader {
33  
34      private static final Logger logger = LoggerFactory.getLogger(CsvSecureMapReader.class);
35  
36      private static final String HMAC = "HMAC";
37      private static final String SIGNATURE = "SIGNATURE";
38  
39      private ICsvMapReader delegate;
40  
41      /**
42       * Constructs a new CsvHmacWriter.
43       *
44       * @param delegate the real CsvMapReader to read from.
45       */
46      public CsvSecureMapReader(ICsvMapReader delegate) {
47          this.delegate = delegate;
48      }
49  
50      @Override
51      public void close() throws IOException {
52          delegate.close();
53      }
54  
55      @Override
56      public String get(int n) {
57          return delegate.get(n);
58      }
59  
60      @Override
61      public Map<String, String> read(String... nameMapping) throws IOException {
62          Map<String, Object> values = read(nameMapping, new CellProcessor[nameMapping.length]);
63  
64          Map<String, String> result = new HashMap<>(values.size());
65          for (Map.Entry<String, Object> entry : values.entrySet()) {
66              String key = entry.getKey();
67              Object value = entry.getValue();
68  
69              result.put(key, value == null ? null : value.toString());
70          }
71  
72          return result;
73      }
74  
75      @Override
76      public String[] getHeader(boolean firstLineCheck) throws IOException {
77          String[] header = delegate.getHeader(firstLineCheck);
78          if (header == null) {
79              return null;
80          }
81          // The 2 last columns are the HMAC and SIGNATURE one.
82          String[] result = new String[header.length - 2];
83          System.arraycopy(header, 0, result, 0, result.length);
84  
85          return result;
86      }
87  
88      @Override
89      public Map<String, Object> read(String[] nameMapping, CellProcessor[] processors) throws IOException {
90          String[] newNameMapping = addExtraColumn(nameMapping);
91  
92          CellProcessor[] newProcessors = new CellProcessor[newNameMapping.length];
93          System.arraycopy(processors, 0, newProcessors, 0, processors.length);
94          newProcessors[processors.length] = null;
95          newProcessors[processors.length + 1] = null;
96  
97          return dropExtraColumns(delegate.read(newNameMapping, newProcessors));
98      }
99  
100     @Override
101     public int getLineNumber() {
102         return delegate.getLineNumber();
103     }
104 
105     @Override
106     public String getUntokenizedRow() {
107         return delegate.getUntokenizedRow();
108     }
109 
110     @Override
111     public int getRowNumber() {
112         return delegate.getRowNumber();
113     }
114 
115     @Override
116     public int length() {
117         return delegate.length();
118     }
119 
120     private String[] addExtraColumn(String... header) {
121         String[] newHeader = new String[header.length + 2];
122         System.arraycopy(header, 0, newHeader, 0, header.length);
123         newHeader[header.length] = HMAC;
124         newHeader[header.length + 1] = SIGNATURE;
125         return newHeader;
126     }
127 
128     private <T> Map<String, T> dropExtraColumns(Map<String, T> source) {
129         if (source != null) {
130             source.remove(HMAC);
131             source.remove(SIGNATURE);
132         }
133         return source;
134     }
135 }