forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotationSecretProvider.java
More file actions
162 lines (140 loc) · 4.63 KB
/
RotationSecretProvider.java
File metadata and controls
162 lines (140 loc) · 4.63 KB
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
package act.session;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2018 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.App;
import act.conf.AppConfig;
import org.joda.time.DateTime;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
@Singleton
public class RotationSecretProvider {
private String rawSecret;
private String lastSecret;
private String curSecret;
private String nextSecret;
private int periodInMinutes;
private boolean rotateEnabled;
@Inject
public RotationSecretProvider(App app) {
AppConfig<?> config = app.config();
rawSecret = config.secret();
rotateEnabled = config.rotateSecret();
if (rotateEnabled) {
int period = config.secretRotatePeriod();
app.jobManager().every("secret-rotator", new Runnable() {
@Override
public void run() {
rotateSecret();
}
}, period, TimeUnit.MINUTES);
periodInMinutes = period;
rotateSecret();
}
}
public boolean isRotateEnabled() {
return rotateEnabled;
}
public String rawSecret() {
return rawSecret;
}
public String curSecret() {
ensureRotateEnabled();
return curSecret;
}
public String lastSecret() {
ensureRotateEnabled();
return lastSecret;
}
public String nextSecret() {
ensureRotateEnabled();
return nextSecret;
}
private static final int[] VALID_MINUTES = {
1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60
};
private static final int[] VALID_HOURS = {
1, 2, 3, 4, 6, 8, 12, 24
};
public static int roundToPeriod(int minutes) {
E.illegalArgumentIf(minutes <= 0);
if (minutes > 60 * 24) {
return 60 * 24;
} else if (minutes > 60) {
float f = minutes / 60f;
int hours = Math.round(f);
int pos = Arrays.binarySearch(VALID_HOURS, hours);
return (pos >= 0 ? hours : VALID_HOURS[-(pos + 1)]) * 60;
} if (minutes > 30) {
int reminder = minutes % 30;
return 0 == reminder ? minutes : minutes + (reminder < 15 ? -reminder : (30 - reminder));
} else {
int pos = Arrays.binarySearch(VALID_MINUTES, minutes);
return pos >= 0 ? minutes : VALID_MINUTES[-(pos + 1)];
}
}
private void rotateSecret() {
ensureRotateEnabled();
lastSecret = curSecret;
curSecret = null != nextSecret ? nextSecret : calculateCurrentSecret();
nextSecret = calculateNextSecret();
if (null == lastSecret) {
lastSecret = curSecret;
}
}
private String calculateCurrentSecret() {
return rawSecret + extension(0);
}
private String calculateNextSecret() {
return rawSecret + extension(1);
}
private String extension(int forwardStep) {
long trait;
if (periodInMinutes > 60) {
int periodInHours = periodInMinutes / 60;
int hoursOfDay = DateTime.now().hourOfDay().get();
int period = hoursOfDay / periodInHours;
trait = currentHourStart().getMillis() / 1000 / 60;
if (0 < period) {
trait += (period * 60);
}
} else {
int minutesOfHour = DateTime.now().minuteOfHour().get();
int period = minutesOfHour / periodInMinutes;
trait = currentHourStart().getMillis() / 1000 / 60;
if (0 < period) {
trait += period;
}
}
if (forwardStep > 0) {
trait += forwardStep * periodInMinutes;
}
return S.string(trait);
}
private void ensureRotateEnabled() {
E.illegalStateIfNot(rotateEnabled, "secret rotate not enabled");
}
private static DateTime currentHourStart() {
return DateTime.now().withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
}
}