Skip to content

Commit d497e5e

Browse files
fix(core): fix the issues caused due to the use of SpinnakerRetrofitErrorHandler in building FiatService (#1838)
* refactor(core): Convert PermissionServiceSpec tests from groovy to java in a new class PermissionServiceTest * fix(core): fix the issues caused due to the use of SpinnakerRetrofitErrorHandler in building FiatService * fix(test): remove conversion errors with error codes not in [200, 300) as they never occur --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent bb10acf commit d497e5e

File tree

5 files changed

+361
-182
lines changed

5 files changed

+361
-182
lines changed

gate-core/gate-core.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
implementation "io.spinnaker.fiat:fiat-core:$fiatVersion"
3434

3535
implementation "io.spinnaker.kork:kork-core"
36+
implementation "io.spinnaker.kork:kork-retrofit"
3637
implementation "io.spinnaker.kork:kork-web"
3738
implementation "io.spinnaker.kork:kork-security"
3839
implementation "com.netflix.spectator:spectator-api"

gate-core/src/main/java/com/netflix/spinnaker/gate/retrofit/UpstreamBadRequest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static retrofit.RetrofitError.Kind.HTTP;
2121

2222
import com.netflix.spinnaker.kork.exceptions.SpinnakerException;
23+
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException;
24+
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException;
2325
import java.util.Collection;
2426
import retrofit.RetrofitError;
2527

@@ -36,6 +38,14 @@ private UpstreamBadRequest(RetrofitError cause) {
3638
error = cause.getBody();
3739
}
3840

41+
private UpstreamBadRequest(SpinnakerHttpException cause) {
42+
super(cause.getMessage(), cause);
43+
this.setRetryable(cause.getRetryable());
44+
status = cause.getResponseCode();
45+
url = cause.getUrl();
46+
error = cause.getResponseBody();
47+
}
48+
3949
public int getStatus() {
4050
return status;
4151
}
@@ -66,4 +76,13 @@ public static RuntimeException classifyError(
6676
return error;
6777
}
6878
}
79+
80+
public static RuntimeException classifyError(SpinnakerServerException error) {
81+
if (error instanceof SpinnakerHttpException
82+
&& ((SpinnakerHttpException) error).getResponseCode() < INTERNAL_SERVER_ERROR.value()) {
83+
return new UpstreamBadRequest((SpinnakerHttpException) error);
84+
} else {
85+
return error;
86+
}
87+
}
6988
}

gate-core/src/main/java/com/netflix/spinnaker/gate/services/PermissionService.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.netflix.spinnaker.kork.core.RetrySupport;
3030
import com.netflix.spinnaker.kork.exceptions.SpinnakerException;
3131
import com.netflix.spinnaker.kork.exceptions.SystemException;
32+
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException;
33+
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException;
3234
import com.netflix.spinnaker.security.AuthenticatedRequest;
3335
import com.netflix.spinnaker.security.User;
3436
import java.time.Duration;
@@ -47,7 +49,6 @@
4749
import org.springframework.stereotype.Component;
4850
import org.springframework.util.CollectionUtils;
4951
import org.springframework.util.StringUtils;
50-
import retrofit.RetrofitError;
5152

5253
@Log4j2
5354
@Component
@@ -83,7 +84,7 @@ public void login(final String userId) {
8384
permissionEvaluator.invalidatePermission(userId);
8485
return null;
8586
});
86-
} catch (RetrofitError e) {
87+
} catch (SpinnakerServerException e) {
8788
throw UpstreamBadRequest.classifyError(e);
8889
}
8990
}
@@ -98,7 +99,7 @@ public void loginWithRoles(final String userId, final Collection<String> roles)
9899
permissionEvaluator.invalidatePermission(userId);
99100
return null;
100101
});
101-
} catch (RetrofitError e) {
102+
} catch (SpinnakerServerException e) {
102103
throw UpstreamBadRequest.classifyError(e);
103104
}
104105
}
@@ -109,7 +110,7 @@ public void logout(String userId) {
109110
try {
110111
getFiatServiceForLogin().logoutUser(userId);
111112
permissionEvaluator.invalidatePermission(userId);
112-
} catch (RetrofitError e) {
113+
} catch (SpinnakerServerException e) {
113114
throw UpstreamBadRequest.classifyError(e);
114115
}
115116
}
@@ -119,7 +120,7 @@ public void sync() {
119120
if (fiatStatus.isEnabled()) {
120121
try {
121122
getFiatServiceForLogin().sync(List.of());
122-
} catch (RetrofitError e) {
123+
} catch (SpinnakerServerException e) {
123124
throw UpstreamBadRequest.classifyError(e);
124125
}
125126
}
@@ -133,22 +134,22 @@ public Set<Role.View> getRoles(String userId) {
133134
var permission = permissionEvaluator.getPermission(userId);
134135
var roles = permission != null ? permission.getRoles() : null;
135136
return roles != null ? roles : Set.of();
136-
} catch (RetrofitError e) {
137+
} catch (SpinnakerServerException e) {
137138
throw UpstreamBadRequest.classifyError(e);
138139
}
139140
}
140141

141142
List<UserPermission.View> lookupServiceAccounts(String userId) {
142143
try {
143144
return extendedFiatService.getUserServiceAccounts(userId);
144-
} catch (RetrofitError re) {
145-
var response = re.getResponse();
146-
if (response != null && response.getStatus() == HttpStatus.NOT_FOUND.value()) {
145+
} catch (SpinnakerHttpException e) {
146+
if (e.getResponseCode() == 404) {
147147
return List.of();
148148
}
149-
boolean shouldRetry =
150-
response == null || HttpStatus.valueOf(response.getStatus()).is5xxServerError();
151-
throw new SystemException("getUserServiceAccounts failed", re).setRetryable(shouldRetry);
149+
boolean shouldRetry = HttpStatus.valueOf(e.getResponseCode()).is5xxServerError();
150+
throw new SystemException("getUserServiceAccounts failed", e).setRetryable(shouldRetry);
151+
} catch (SpinnakerServerException e) {
152+
throw new SystemException("getUserServiceAccounts failed", e).setRetryable(e.getRetryable());
152153
}
153154
}
154155

@@ -217,8 +218,8 @@ public List<String> getServiceAccounts(@SpinnakerUser User user) {
217218
return permission.getServiceAccounts().stream()
218219
.map(ServiceAccount.View::getName)
219220
.collect(Collectors.toList());
220-
} catch (RetrofitError re) {
221-
throw UpstreamBadRequest.classifyError(re);
221+
} catch (SpinnakerServerException e) {
222+
throw UpstreamBadRequest.classifyError(e);
222223
}
223224
}
224225

gate-core/src/test/groovy/com/netflix/spinnaker/gate/services/PermissionServiceSpec.groovy

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)