2019-06-19 20:01:49 +02:00
|
|
|
package lu.circl.mispbump.auxiliary;
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-07-24 12:16:51 +02:00
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-07-14 18:18:54 +02:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-06-04 20:48:24 +02:00
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
import java.net.NoRouteToHostException;
|
2019-05-27 16:06:07 +02:00
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
2019-06-17 09:49:09 +02:00
|
|
|
import javax.net.ssl.SSLHandshakeException;
|
2019-05-27 16:06:07 +02:00
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
2019-08-22 16:58:50 +02:00
|
|
|
import lu.circl.mispbump.interfaces.MispService;
|
2019-06-19 20:01:49 +02:00
|
|
|
import lu.circl.mispbump.models.restModels.MispOrganisation;
|
2019-07-16 14:53:36 +02:00
|
|
|
import lu.circl.mispbump.models.restModels.MispRole;
|
2019-06-19 20:01:49 +02:00
|
|
|
import lu.circl.mispbump.models.restModels.MispServer;
|
|
|
|
import lu.circl.mispbump.models.restModels.MispUser;
|
|
|
|
import lu.circl.mispbump.models.restModels.Organisation;
|
2019-07-16 14:53:36 +02:00
|
|
|
import lu.circl.mispbump.models.restModels.Role;
|
2019-06-19 20:01:49 +02:00
|
|
|
import lu.circl.mispbump.models.restModels.Server;
|
|
|
|
import lu.circl.mispbump.models.restModels.User;
|
|
|
|
import lu.circl.mispbump.models.restModels.Version;
|
2019-05-27 16:06:07 +02:00
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
2019-07-24 12:16:51 +02:00
|
|
|
|
2019-05-27 20:03:27 +02:00
|
|
|
/**
|
|
|
|
* Implementation of the RetroFit2 Misp client.
|
|
|
|
* In order to conveniently use this api some wrapper interfaces are implemented to return the requested API endpoint as java object.
|
|
|
|
*/
|
2019-05-27 16:06:07 +02:00
|
|
|
public class MispRestClient {
|
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
|
2019-06-19 17:24:00 +02:00
|
|
|
private static MispRestClient instance;
|
2019-08-22 16:58:50 +02:00
|
|
|
private MispService mispService;
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-07-14 18:18:54 +02:00
|
|
|
public static MispRestClient getInstance(String url, String authkey) {
|
2019-06-19 17:24:00 +02:00
|
|
|
if (instance == null) {
|
2019-07-14 18:18:54 +02:00
|
|
|
instance = new MispRestClient();
|
2019-06-19 17:24:00 +02:00
|
|
|
}
|
|
|
|
|
2019-07-14 18:18:54 +02:00
|
|
|
instance.initMispRestInterface(url, authkey);
|
2019-07-05 03:45:04 +02:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2019-07-23 14:58:07 +02:00
|
|
|
private void initMispRestInterface(String url, String authkey) {
|
2019-05-27 16:06:07 +02:00
|
|
|
try {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
2019-06-04 20:48:24 +02:00
|
|
|
.baseUrl(url)
|
2019-05-27 16:06:07 +02:00
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
2019-07-23 14:58:07 +02:00
|
|
|
.client(getCustomClient(true, true, authkey))
|
2019-05-27 16:06:07 +02:00
|
|
|
.build();
|
|
|
|
|
2019-08-22 16:58:50 +02:00
|
|
|
mispService = retrofit.create(MispService.class);
|
2019-05-27 16:06:07 +02:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:45:04 +02:00
|
|
|
|
2019-08-22 16:58:50 +02:00
|
|
|
public MispService getService() {
|
|
|
|
return mispService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
/**
|
2019-07-05 03:45:04 +02:00
|
|
|
* @param unsafe whether to accept all certificates or only trusted ones
|
2019-06-19 20:01:49 +02:00
|
|
|
* @param logging whether to log Retrofit calls (for debugging)
|
|
|
|
* @return {@link OkHttpClient}
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
2019-07-14 18:18:54 +02:00
|
|
|
private OkHttpClient getCustomClient(boolean unsafe, boolean logging, final String authkey) {
|
2019-05-27 16:06:07 +02:00
|
|
|
try {
|
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
if (unsafe) {
|
|
|
|
// Create a trust manager that does not validate certificate chains
|
|
|
|
final TrustManager[] trustAllCerts = new TrustManager[]{
|
|
|
|
new X509TrustManager() {
|
|
|
|
@SuppressLint("TrustAllX509TrustManager")
|
|
|
|
@Override
|
2019-08-22 18:09:45 +02:00
|
|
|
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
|
2019-06-19 20:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("TrustAllX509TrustManager")
|
|
|
|
@Override
|
2019-08-22 18:09:45 +02:00
|
|
|
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
|
2019-06-19 20:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
|
return new java.security.cert.X509Certificate[]{};
|
|
|
|
}
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
2019-06-19 20:01:49 +02:00
|
|
|
};
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
// Install the all-trusting trust manager
|
|
|
|
final SSLContext sslContext = SSLContext.getInstance("SSL");
|
|
|
|
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
// Create an ssl socket factory with our all-trusting manager
|
|
|
|
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-07-05 03:45:04 +02:00
|
|
|
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
|
2019-08-22 18:09:45 +02:00
|
|
|
builder.hostnameVerifier((hostname, session) -> true);
|
2019-06-19 20:01:49 +02:00
|
|
|
}
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
if (logging) {
|
|
|
|
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
|
|
|
|
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
|
|
builder.addInterceptor(interceptor);
|
|
|
|
}
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-08-22 18:09:45 +02:00
|
|
|
// create interceptor
|
|
|
|
builder.addInterceptor(chain -> {
|
|
|
|
Request.Builder ongoing = chain.request().newBuilder();
|
|
|
|
ongoing.addHeader("Accept", "application/json");
|
|
|
|
ongoing.addHeader("Content-Type", "application/json");
|
|
|
|
ongoing.addHeader("Authorization", authkey);
|
|
|
|
return chain.proceed(ongoing.build());
|
2019-05-27 16:06:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return builder.build();
|
2019-06-19 20:01:49 +02:00
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check via pyMispRoute if server is available
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link AvailableCallback}
|
|
|
|
*/
|
|
|
|
public void isAvailable(final AvailableCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<Version> call = mispService.pyMispVersion();
|
2019-06-04 20:48:24 +02:00
|
|
|
call.enqueue(new Callback<Version>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<Version> call, @NonNull Response<Version> response) {
|
2019-06-04 20:48:24 +02:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
if (response.code() == 403) {
|
|
|
|
callback.available();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
callback.unavailable(extractError(response));
|
2019-06-04 20:48:24 +02:00
|
|
|
} else {
|
|
|
|
callback.available();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<Version> call, @NonNull Throwable t) {
|
2019-06-17 09:49:09 +02:00
|
|
|
callback.unavailable(extractError(t));
|
2019-06-04 20:48:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 14:53:36 +02:00
|
|
|
public void getRoles(final AllRolesCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<List<MispRole>> call = mispService.getRoles();
|
2019-07-16 14:53:36 +02:00
|
|
|
call.enqueue(new Callback<List<MispRole>>() {
|
|
|
|
@Override
|
2019-08-22 18:09:45 +02:00
|
|
|
public void onResponse(@NonNull Call<List<MispRole>> call, @NonNull Response<List<MispRole>> response) {
|
2019-07-16 14:53:36 +02:00
|
|
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
callback.failure(extractError(response));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MispRole> mispRoles = response.body();
|
|
|
|
assert mispRoles != null;
|
|
|
|
|
|
|
|
Role[] roles = new Role[mispRoles.size()];
|
|
|
|
|
|
|
|
for (int i = 0; i < roles.length; i++) {
|
|
|
|
roles[i] = mispRoles.get(i).role;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.success(roles);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-08-22 18:09:45 +02:00
|
|
|
public void onFailure(@NonNull Call<List<MispRole>> call, @NonNull Throwable t) {
|
2019-07-16 14:53:36 +02:00
|
|
|
callback.failure(extractError(t));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
/**
|
|
|
|
* Fetches information about the user that is associated with saved auth key.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link UserCallback} wrapper to return user directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
|
|
|
public void getMyUser(final UserCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<MispUser> call = mispService.getMyUserInformation();
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<MispUser>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<MispUser> call, @NonNull Response<MispUser> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
2019-05-27 16:06:07 +02:00
|
|
|
} else {
|
|
|
|
if (response.body() != null) {
|
2019-08-23 13:51:09 +02:00
|
|
|
callback.success(response.body().getUser());
|
2019-05-27 16:06:07 +02:00
|
|
|
} else {
|
|
|
|
callback.failure("response body was null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<MispUser> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
t.printStackTrace();
|
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an user with specific ID.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
|
|
|
* @param userId user identifier
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link UserCallback} wrapper to return user directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
|
|
|
public void getUser(int userId, final UserCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<MispUser> call = mispService.getUser(userId);
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<MispUser>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<MispUser> call, @NonNull Response<MispUser> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
2019-05-27 16:06:07 +02:00
|
|
|
} else {
|
|
|
|
if (response.body() != null) {
|
2019-08-23 13:51:09 +02:00
|
|
|
callback.success(response.body().getUser());
|
2019-05-27 16:06:07 +02:00
|
|
|
} else {
|
|
|
|
callback.failure("response body was null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<MispUser> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
t.printStackTrace();
|
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
public void getUser(final String emailAddress, final UserCallback callback) {
|
|
|
|
getAllUsers(new AllUsersCallback() {
|
|
|
|
@Override
|
|
|
|
public void success(User[] users) {
|
|
|
|
for (User user : users) {
|
2019-07-24 20:32:13 +02:00
|
|
|
if (user.getEmail().equals(emailAddress)) {
|
2019-07-03 23:13:25 +02:00
|
|
|
callback.success(user);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.failure("Could not find user with email address {" + emailAddress + "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void failure(String error) {
|
|
|
|
callback.failure(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:51:09 +02:00
|
|
|
public void getAllUsers(final AllMispUsersCallback callback) {
|
|
|
|
Call<List<MispUser>> call = mispService.getAllUsers();
|
|
|
|
|
|
|
|
call.enqueue(new Callback<List<MispUser>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<MispUser>> call, @NonNull Response<List<MispUser>> response) {
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
callback.failure("Failed onResponse");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.success(response.body());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<MispUser>> call, @NonNull Throwable t) {
|
|
|
|
callback.failure(extractError(t));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
public void getAllUsers(final AllUsersCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<List<MispUser>> call = mispService.getAllUsers();
|
2019-07-03 23:13:25 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<MispUser>>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<MispUser>> call, @NonNull Response<List<MispUser>> response) {
|
2019-07-03 23:13:25 +02:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
callback.failure("Failed onResponse");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MispUser> mispUsers = response.body();
|
|
|
|
assert mispUsers != null;
|
|
|
|
|
|
|
|
User[] users = new User[mispUsers.size()];
|
|
|
|
|
|
|
|
for (int i = 0; i < users.length; i++) {
|
2019-08-23 13:51:09 +02:00
|
|
|
users[i] = mispUsers.get(i).getUser();
|
2019-07-03 23:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback.success(users);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<MispUser>> call, @NonNull Throwable t) {
|
2019-07-03 23:13:25 +02:00
|
|
|
callback.failure(extractError(t));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
/**
|
|
|
|
* Add a given user to the MISP instance referenced by url in preferences.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
|
|
|
* @param user user to add
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link UserCallback} wrapper to return the created user directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
|
|
|
public void addUser(User user, final UserCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<MispUser> call = mispService.addUser(user);
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<MispUser>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<MispUser> call, @NonNull Response<MispUser> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
|
|
|
} else {
|
2019-06-17 09:49:09 +02:00
|
|
|
assert response.body() != null;
|
2019-08-23 13:51:09 +02:00
|
|
|
callback.success(response.body().getUser());
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<MispUser> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
// --- organisation routes ---
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an organisation by a given organisation id.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
|
|
|
* @param orgId organisation identifier
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link OrganisationCallback} wrapper to return a organisation directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
|
|
|
public void getOrganisation(int orgId, final OrganisationCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<MispOrganisation> call = mispService.getOrganisation(orgId);
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<MispOrganisation>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<MispOrganisation> call, @NonNull Response<MispOrganisation> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
2019-05-27 16:06:07 +02:00
|
|
|
} else {
|
|
|
|
if (response.body() != null) {
|
|
|
|
callback.success(response.body().organisation);
|
|
|
|
} else {
|
2019-07-23 14:58:07 +02:00
|
|
|
callback.failure("Response was empty");
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<MispOrganisation> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
public void getOrganisation(final String uuid, final OrganisationCallback callback) {
|
|
|
|
getAllOrganisations(new AllOrganisationsCallback() {
|
|
|
|
@Override
|
|
|
|
public void success(Organisation[] organisations) {
|
|
|
|
for (Organisation organisation : organisations) {
|
2019-07-22 14:43:12 +02:00
|
|
|
if (organisation.getUuid().equals(uuid)) {
|
2019-07-03 23:13:25 +02:00
|
|
|
callback.success(organisation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.failure("Could not find organisation with UUID {" + uuid + "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void failure(String error) {
|
|
|
|
callback.failure(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getAllOrganisations(final AllOrganisationsCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<List<MispOrganisation>> call = mispService.getAllOrganisations();
|
2019-06-17 09:49:09 +02:00
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
call.enqueue(new Callback<List<MispOrganisation>>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<MispOrganisation>> call, @NonNull Response<List<MispOrganisation>> response) {
|
2019-07-03 23:13:25 +02:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 09:49:09 +02:00
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
List<MispOrganisation> mispOrganisations = response.body();
|
|
|
|
|
|
|
|
assert mispOrganisations != null;
|
2019-06-17 09:49:09 +02:00
|
|
|
|
2019-07-03 23:13:25 +02:00
|
|
|
Organisation[] organisations = new Organisation[mispOrganisations.size()];
|
|
|
|
|
|
|
|
for (int i = 0; i < mispOrganisations.size(); i++) {
|
|
|
|
organisations[i] = mispOrganisations.get(i).organisation;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.success(organisations);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<MispOrganisation>> call, @NonNull Throwable t) {
|
2019-07-03 23:13:25 +02:00
|
|
|
callback.failure(extractError(t));
|
|
|
|
}
|
|
|
|
});
|
2019-06-17 09:49:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
/**
|
|
|
|
* Add a given organisation to the MISP instance referenced by url in preferences.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
2019-05-27 16:06:07 +02:00
|
|
|
* @param organisation organisation to add
|
2019-06-17 09:49:09 +02:00
|
|
|
* @param callback {@link OrganisationCallback} wrapper to return the created organisation directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
|
|
|
public void addOrganisation(Organisation organisation, final OrganisationCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<MispOrganisation> call = mispService.addOrganisation(organisation);
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<MispOrganisation>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<MispOrganisation> call, @NonNull Response<MispOrganisation> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
|
|
|
} else {
|
2019-06-17 09:49:09 +02:00
|
|
|
assert response.body() != null;
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.success(response.body().organisation);
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<MispOrganisation> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
// --- server routes ---
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all servers on MISP instance.
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link OrganisationCallback} wrapper to return a list of servers directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
2019-07-03 23:13:25 +02:00
|
|
|
public void getAllServers(final AllServersCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<List<MispServer>> call = mispService.getAllServers();
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<MispServer>>() {
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<MispServer>> call, @NonNull Response<List<MispServer>> response) {
|
2019-06-17 09:49:09 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
|
|
|
} else {
|
2019-07-03 23:13:25 +02:00
|
|
|
List<MispServer> mispServers = response.body();
|
|
|
|
assert mispServers != null;
|
|
|
|
|
|
|
|
Server[] servers = new Server[mispServers.size()];
|
|
|
|
|
|
|
|
for (int i = 0; i < servers.length; i++) {
|
2019-07-24 20:32:13 +02:00
|
|
|
servers[i] = mispServers.get(i).getServer();
|
2019-07-03 23:13:25 +02:00
|
|
|
}
|
|
|
|
callback.success(servers);
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<MispServer>> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:32:13 +02:00
|
|
|
public void getAllServers(final AllRawServersCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<List<MispServer>> call = mispService.getAllServers();
|
2019-07-24 20:32:13 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<MispServer>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<MispServer>> call, @NonNull Response<List<MispServer>> response) {
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
callback.failure(extractError(response));
|
|
|
|
} else {
|
|
|
|
callback.success(response.body());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<MispServer>> call, @NonNull Throwable t) {
|
|
|
|
callback.failure(t.getMessage());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
/**
|
|
|
|
* Add a server to the MISP instance
|
2019-06-17 09:49:09 +02:00
|
|
|
*
|
|
|
|
* @param server the server to create
|
2019-06-04 20:48:24 +02:00
|
|
|
* @param callback {@link ServerCallback} wrapper to return the created server directly
|
2019-05-27 16:06:07 +02:00
|
|
|
*/
|
2019-06-04 20:48:24 +02:00
|
|
|
public void addServer(Server server, final ServerCallback callback) {
|
2019-08-22 16:58:50 +02:00
|
|
|
Call<Server> call = mispService.addServer(server);
|
2019-06-04 20:48:24 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<Server>() {
|
2019-05-27 16:06:07 +02:00
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onResponse(@NonNull Call<Server> call, @NonNull Response<Server> response) {
|
2019-05-27 16:06:07 +02:00
|
|
|
if (!response.isSuccessful()) {
|
2019-06-04 20:48:24 +02:00
|
|
|
callback.failure(extractError(response));
|
|
|
|
} else {
|
|
|
|
callback.success(response.body());
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-14 18:18:54 +02:00
|
|
|
public void onFailure(@NonNull Call<Server> call, @NonNull Throwable t) {
|
2019-05-27 16:06:07 +02:00
|
|
|
callback.failure(t.getMessage());
|
2019-07-03 23:13:25 +02:00
|
|
|
throw new RuntimeException(t);
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-06-04 20:48:24 +02:00
|
|
|
|
2019-06-19 20:01:49 +02:00
|
|
|
// --- error parsing ---
|
2019-06-17 09:49:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts error {@link Response}s to human readable info.
|
2019-07-05 03:45:04 +02:00
|
|
|
*
|
2019-06-17 09:49:09 +02:00
|
|
|
* @param response erroneous response
|
2019-07-05 03:45:04 +02:00
|
|
|
* @param <T> type of response
|
2019-06-17 09:49:09 +02:00
|
|
|
* @return human readable String that describes the error
|
|
|
|
*/
|
2019-06-04 20:48:24 +02:00
|
|
|
private <T> String extractError(Response<T> response) {
|
|
|
|
switch (response.code()) {
|
|
|
|
// bad request (malformed)
|
|
|
|
case 400:
|
|
|
|
return "Bad request";
|
|
|
|
|
|
|
|
// unauthorized
|
|
|
|
case 401:
|
|
|
|
return "Unauthorized";
|
|
|
|
|
|
|
|
// forbidden
|
|
|
|
case 403:
|
2019-07-23 16:57:06 +02:00
|
|
|
return "Authentification failed";
|
2019-06-04 20:48:24 +02:00
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
// not found
|
2019-06-04 20:48:24 +02:00
|
|
|
case 404:
|
|
|
|
return "Not found";
|
|
|
|
|
2019-07-23 16:57:06 +02:00
|
|
|
// No permission (method not allowed)
|
|
|
|
case 405:
|
|
|
|
return "No admin permission";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return response.message();
|
|
|
|
}
|
2019-06-04 20:48:24 +02:00
|
|
|
}
|
2019-06-17 09:49:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a {@link Throwable} to a human readable error message.
|
2019-07-05 03:45:04 +02:00
|
|
|
*
|
2019-06-17 09:49:09 +02:00
|
|
|
* @param t throwable
|
|
|
|
* @return human readable String that describes the error.
|
|
|
|
*/
|
|
|
|
private String extractError(Throwable t) {
|
|
|
|
|
|
|
|
if (t.getCause() instanceof CertificateException) {
|
|
|
|
return "Trust anchor for certification path not found.\nSelf signed certificates are not supported.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t instanceof SSLHandshakeException) {
|
|
|
|
return "SSL Handshake Error";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t instanceof NoRouteToHostException) {
|
|
|
|
return "Server is not available (no route to host)";
|
|
|
|
}
|
|
|
|
|
2019-07-23 14:58:07 +02:00
|
|
|
try {
|
|
|
|
throw new Exception(t);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
return t.getMessage();
|
|
|
|
}
|
2019-07-14 18:18:54 +02:00
|
|
|
|
|
|
|
// interfaces
|
|
|
|
public interface AvailableCallback {
|
|
|
|
void available();
|
|
|
|
|
|
|
|
void unavailable(String error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface UserCallback {
|
|
|
|
void success(User user);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface AllUsersCallback {
|
|
|
|
void success(User[] users);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:51:09 +02:00
|
|
|
public interface AllMispUsersCallback {
|
|
|
|
void success(List<MispUser> users);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
2019-07-14 18:18:54 +02:00
|
|
|
public interface OrganisationCallback {
|
|
|
|
void success(Organisation organisation);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface AllOrganisationsCallback {
|
|
|
|
void success(Organisation[] organisations);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface ServerCallback {
|
|
|
|
void success(Server server);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface AllServersCallback {
|
|
|
|
void success(Server[] servers);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
2019-07-16 14:53:36 +02:00
|
|
|
|
2019-07-24 20:32:13 +02:00
|
|
|
public interface AllRawServersCallback {
|
|
|
|
void success(List<MispServer> mispServers);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
|
|
|
|
2019-07-16 14:53:36 +02:00
|
|
|
public interface AllRolesCallback {
|
|
|
|
void success(Role[] roles);
|
|
|
|
|
|
|
|
void failure(String error);
|
|
|
|
}
|
2019-07-24 12:16:51 +02:00
|
|
|
}
|