aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa-authn/src/main/java/org/opendaylight/aaa/SecureBlockingQueue.java
blob: 3ded52daa396b8c89109df2ddae46c8c0c03a9c9 (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
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.aaa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.opendaylight.aaa.api.Authentication;

/**
 * A {@link BlockingQueue} decorator with injected security context.
 *
 * @author liemmn
 *
 * @param <T>
 *            queue element type
 */
public class SecureBlockingQueue<T> implements BlockingQueue<T> {
    private final BlockingQueue<SecureData<T>> queue;

    /**
     * Constructor.
     *
     * @param queue
     *            blocking queue implementation to use
     */
    public SecureBlockingQueue(BlockingQueue<SecureData<T>> queue) {
        this.queue = queue;
    }

    @Override
    public T remove() {
        return setAuth(queue.remove());
    }

    @Override
    public T poll() {
        return setAuth(queue.poll());
    }

    @Override
    public T element() {
        return setAuth(queue.element());
    }

    @Override
    public T peek() {
        return setAuth(queue.peek());
    }

    @Override
    public int size() {
        return queue.size();
    }

    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            Iterator<SecureData<T>> it = queue.iterator();

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public T next() {
                return it.next().data;
            }

            @Override
            public void remove() {
                it.remove();
            }
        };
    }

    @Override
    public Object[] toArray() {
        return toData().toArray();
    }

    @SuppressWarnings("hiding")
    @Override
    public <T> T[] toArray(T[] a) {
        return toData().toArray(a);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return toData().containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        return queue.addAll(fromData(c));
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return queue.removeAll(fromData(c));
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return queue.retainAll(fromData(c));
    }

    @Override
    public void clear() {
        queue.clear();
    }

    @Override
    public boolean add(T e) {
        return queue.add(new SecureData<>(e));
    }

    @Override
    public boolean offer(T e) {
        return queue.offer(new SecureData<>(e));
    }

    @Override
    public void put(T e) throws InterruptedException {
        queue.put(new SecureData<T>(e));
    }

    @Override
    public boolean offer(T e, long timeout, TimeUnit unit) throws InterruptedException {
        return queue.offer(new SecureData<>(e), timeout, unit);
    }

    @Override
    public T take() throws InterruptedException {
        return setAuth(queue.take());
    }

    @Override
    public T poll(long timeout, TimeUnit unit) throws InterruptedException {
        return setAuth(queue.poll(timeout, unit));
    }

    @Override
    public int remainingCapacity() {
        return queue.remainingCapacity();
    }

    @Override
    public boolean remove(Object o) {
        Iterator<SecureData<T>> it = queue.iterator();
        while (it.hasNext()) {
            SecureData<T> sd = it.next();
            if (sd.data.equals(o)) {
                return queue.remove(sd);
            }
        }
        return false;
    }

    @Override
    public boolean contains(Object o) {
        Iterator<SecureData<T>> it = queue.iterator();
        while (it.hasNext()) {
            SecureData<T> sd = it.next();
            if (sd.data.equals(o)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int drainTo(Collection<? super T> c) {
        Collection<SecureData<T>> sd = new ArrayList<>();
        int n = queue.drainTo(sd);
        c.addAll(toData(sd));
        return n;
    }

    @Override
    public int drainTo(Collection<? super T> c, int maxElements) {
        Collection<SecureData<T>> sd = new ArrayList<>();
        int n = queue.drainTo(sd, maxElements);
        c.addAll(toData(sd));
        return n;
    }

    // Rehydrate security context
    private T setAuth(SecureData<T> i) {
        AuthenticationManager.instance().set(i.auth);
        return i.data;
    }

    // Construct secure data collection from a plain old data collection
    @SuppressWarnings("unchecked")
    private Collection<SecureData<T>> fromData(Collection<?> c) {
        Collection<SecureData<T>> sd = new ArrayList<>(c.size());
        for (Object d : c) {
            sd.add((SecureData<T>) new SecureData<>(d));
        }
        return sd;
    }

    // Extract the data portion out from the secure data
    @SuppressWarnings("unchecked")
    private Collection<T> toData() {
        return toData(Arrays.<SecureData<T>> asList(queue.toArray(new SecureData[0])));
    }

    // Extract the data portion out from the secure data
    private Collection<T> toData(Collection<SecureData<T>> secureData) {
        Collection<T> data = new ArrayList<>(secureData.size());
        Iterator<SecureData<T>> it = secureData.iterator();
        while (it.hasNext()) {
            data.add(it.next().data);
        }
        return data;
    }

    // Inject security context
    public static final class SecureData<T> {
        private final T data;
        private final Authentication auth;

        private SecureData(T data) {
            this.data = data;
            this.auth = AuthenticationManager.instance().get();
        }

        @SuppressWarnings("rawtypes")
        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }
            return (o instanceof SecureData) ? data.equals(((SecureData) o).data) : false;
        }

        @Override
        public int hashCode() {
            return data.hashCode();
        }
    }
}