You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
logf("Successfully registered keys %v. Keys are updated by the daemon process every %.0f minutes. Check the log for the most recent run.", ks, daemonRefreshTime.Minutes())
139
+
returnnil
140
+
}
141
+
142
+
// fetchAndPrintKey fetches a key from the server and prints it as JSON.
143
+
// This is used by both cached and uncached modes when -g flag is specified.
144
+
//
145
+
// Note: The timeout bounds the total retry time, but individual CacheGetKey calls
146
+
// may block beyond the deadline since CacheGetKey doesn't support context cancellation.
147
+
// The deadline is checked before each retry attempt to minimize overage.
return&ErrorStatus{fmt.Errorf("invalid value for timeout flag: %w", err), false}
152
+
}
153
+
154
+
// Start deadline timer before first call to bound total time
155
+
deadline:=time.After(timeout)
156
+
varkey*knox.Key
157
+
varfetchErrerror// Track fetch errors separately for clarity
158
+
159
+
for {
160
+
// Check timeout before each attempt
161
+
select {
162
+
case<-deadline:
163
+
iffetchErr!=nil {
128
164
return&ErrorStatus{fmt.Errorf(
129
-
"Error getting key from daemon (hit timeout after %s seconds); check knox logs for details (most recent error: %v)",
130
-
timeout.String(), err), false}
131
-
case<-time.After(registerRecheckTime):
132
-
key, err=cli.CacheGetKey(*registerKey)
165
+
"error getting key from server (hit timeout after %s): %w",
166
+
timeout.String(), fetchErr), false}
133
167
}
168
+
// Timeout on first attempt before any fetch was made
169
+
return&ErrorStatus{fmt.Errorf(
170
+
"error getting key from server (hit timeout after %s before fetch attempt)",
171
+
timeout.String()), false}
172
+
default:
173
+
// Continue with fetch attempt
134
174
}
135
-
// TODO: add json vs data option?
136
-
data, err:=json.Marshal(key)
137
-
iferr!=nil {
138
-
return&ErrorStatus{err, true}
175
+
176
+
key, fetchErr=cli.CacheGetKey(keyID)
177
+
iffetchErr==nil {
178
+
break
179
+
}
180
+
181
+
// Wait before retry, but also check deadline
182
+
select {
183
+
case<-deadline:
184
+
return&ErrorStatus{fmt.Errorf(
185
+
"error getting key from server (hit timeout after %s): %w",
186
+
timeout.String(), fetchErr), false}
187
+
case<-time.After(registerRecheckTime):
188
+
// Continue to next attempt
139
189
}
140
-
fmt.Printf("%s", string(data))
141
-
returnnil
142
190
}
143
-
logf("Successfully registered keys %v. Keys are updated by the daemon process every %.0f minutes. Check the log for the most recent run.", ks, daemonRefreshTime.Minutes())
0 commit comments