Skip to content

Commit

Permalink
#2379 implemented initial version of the event publishing from container
Browse files Browse the repository at this point in the history
  • Loading branch information
tjamakeev committed Apr 7, 2018
1 parent cd0eed4 commit 89d6dc6
Show file tree
Hide file tree
Showing 26 changed files with 1,003 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private Session getSession( final Message message, final HttpServletRequest req


//******************************************************************
protected Session authenticateAccess( Message message, HttpServletRequest req )
protected Session authenticateAccess( Message message, HttpServletRequest request )
{
String sptoken;

Expand All @@ -121,14 +121,14 @@ protected Session authenticateAccess( Message message, HttpServletRequest req )
}
else
{
String bearerToken = getBearerToken( req );
String bearerToken = getBearerToken( request );
if ( bearerToken != null )
{
return identityManager.login( bearerToken );
return identityManager.login( request, message );
}
else
{
sptoken = req.getParameter( "sptoken" );
sptoken = request.getParameter( "sptoken" );

if ( Strings.isNullOrEmpty( sptoken ) )
{
Expand All @@ -140,7 +140,7 @@ protected Session authenticateAccess( Message message, HttpServletRequest req )

if ( Strings.isNullOrEmpty( sptoken ) )
{
Cookie[] cookies = req.getCookies();
Cookie[] cookies = request.getCookies();
for ( final Cookie cookie : cookies )
{
if ( "sptoken".equals( cookie.getName() ) )
Expand All @@ -161,6 +161,7 @@ protected Session authenticateAccess( Message message, HttpServletRequest req )
}
}
}

//******************************************************************


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ void updateContainerHostname( final String environmentId, final String container
void placeEnvironmentInfoByContainerId( String environmentId, String containerIp )
throws EnvironmentNotFoundException, ContainerHostNotFoundException, CommandException;

Environment getEnvironment( String environmentId );

/**
* Places JWT token to container
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,13 @@ public void placeEnvironmentInfoByContainerIp( final String containerIp ) throws
}


@Override
public Environment getEnvironment( String environmentId )
{
return environmentService.find( environmentId );
}


@Override
public void placeTokenToContainer( String environmentId, String containerIp, String token )
throws EnvironmentNotFoundException, ContainerHostNotFoundException, CommandException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -983,4 +983,11 @@ public void onContainerDestroyed( final ContainerHost containerHost )
{
environmentManager.onContainerDestroyed( containerHost );
}


@Override
public Environment getEnvironment( final String environmentId )
{
return environmentManager.getEnvironment( environmentId );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


import io.subutai.core.identity.api.exception.TokenCreateException;
import io.subutai.hub.share.dto.environment.EnvironmentInfoDto;
import io.subutai.hub.share.event.Event;


public interface EnvironmentMetadataManager
Expand All @@ -15,4 +17,12 @@ public interface EnvironmentMetadataManager
*/

void issueToken( String containerIp ) throws TokenCreateException;

EnvironmentInfoDto getEnvironmentInfoDto( String environmentId );

/***
* Pushes event to consumers
* @param event @see Event
*/
void pushEvent( Event event );
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.subutai.common.command.CommandException;
import io.subutai.common.environment.Environment;
import io.subutai.common.environment.EnvironmentNotFoundException;
import io.subutai.common.peer.ContainerHost;
import io.subutai.common.peer.HostNotFoundException;
Expand All @@ -13,6 +16,9 @@
import io.subutai.core.identity.api.IdentityManager;
import io.subutai.core.identity.api.exception.TokenCreateException;
import io.subutai.core.peer.api.PeerManager;
import io.subutai.hub.share.dto.environment.EnvironmentInfoDto;
import io.subutai.hub.share.event.Event;
import io.subutai.hub.share.json.JsonUtil;


/**
Expand Down Expand Up @@ -54,13 +60,42 @@ public void issueToken( String containerIp ) throws TokenCreateException
ContainerHost container = peerManager.getLocalPeer().getContainerHostByIp( containerIp );
String environmentId = container.getEnvironmentId().getId();
String containerId = container.getContainerId().getId();
final String token = identityManager.issueJWTToken( environmentId, containerId );
String peerId = container.getPeerId();
String origin = String.format( "%s.%s.%s", peerId, containerId, environmentId );
final String token = identityManager.issueJWTToken( origin );
environmentManager.placeTokenToContainer( environmentId, containerIp, token );
}
catch ( HostNotFoundException | EnvironmentNotFoundException | CommandException e )
{
throw new TokenCreateException( e.getMessage() );
}
}


@Override
public EnvironmentInfoDto getEnvironmentInfoDto( final String environmentId )
{
Environment environment = environmentManager.getEnvironment( environmentId );
final EnvironmentInfoDto result = new EnvironmentInfoDto();
result.setName( environment.getName() );
result.setSubnetCidr( environment.getSubnetCidr() );
return result;
}


@Override
public void pushEvent( final Event event )
{
try
{
LOG.debug( "Event received: {} {}", event, JsonUtil.toJson( event ) );
LOG.debug( "OS: {}", event.getCustomMetaByKey( "OS" ) );
}
catch ( JsonProcessingException e )
{
LOG.error( e.getMessage(), e );
}
// TODO: send event to consumers
}
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package io.subutai.core.environment.metadata.rest;


import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import io.subutai.hub.share.event.EventMessage;


/**
* REST endpoint for environment metadata manager
Expand All @@ -19,5 +26,15 @@ public interface RestService

@Path( "/echo/{message}" )
@GET
Response echo( @PathParam( "message" ) String message );
Response echo( @HeaderParam( "containerId" ) String containerId, @PathParam( "message" ) String message );

@Path( "/info" )
@Produces( MediaType.APPLICATION_JSON )
@GET
Response getEnvironmentDto( @HeaderParam( "environmentId" ) String environmentId );

@Path( "/event" )
@Consumes( MediaType.APPLICATION_JSON )
@POST
Response pushEvent( @HeaderParam( "subutaiOrigin" ) String origin, EventMessage event );
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import io.subutai.core.environment.metadata.api.EnvironmentMetadataManager;
import io.subutai.core.identity.api.exception.TokenCreateException;
import io.subutai.hub.share.dto.environment.EnvironmentInfoDto;
import io.subutai.hub.share.event.EventMessage;


public class RestServiceImpl implements RestService
Expand Down Expand Up @@ -40,8 +42,30 @@ public Response issueToken( String containerIp )


@Override
public Response echo( String message )
public Response echo( final String containerId, String message )
{
return Response.ok( message ).build();
return Response.ok( String.format( "You are %s and your message is %s.", containerId, message ) ).build();
}


@Override
public Response getEnvironmentDto( final String environmentId )
{
EnvironmentInfoDto environmentInfoDto = environmentMetadataManager.getEnvironmentInfoDto( environmentId );
return Response.ok( environmentInfoDto ).build();
}


@Override
public Response pushEvent( final String subutaiOrigin, final EventMessage event )
{
if ( event == null || !subutaiOrigin.equals( event.getOrigin().getId() ) )
{
return Response.status( Response.Status.BAD_REQUEST ).build();
}

event.addTrace( "peer:" + event.getOrigin().getPeerId() );
environmentMetadataManager.pushEvent( event );
return Response.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.concurrent.Callable;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.message.Message;

import io.subutai.common.security.exception.SystemSecurityException;
import io.subutai.common.security.objects.PermissionObject;
Expand Down Expand Up @@ -46,14 +49,16 @@ public interface IdentityManager

void destroy();

TokenHelper buildTokenHelper( String token ) throws TokenParseException;

/**
* Bearer token login
* Token auth login
*
* @param bearerToken bearer token
* @param request HttpServletRequest
*
* @return Session @see Session
*/
Session login( String bearerToken );
Session login( HttpServletRequest request, Message message );

/* *************************************************
*/
Expand Down Expand Up @@ -331,9 +336,9 @@ boolean isUserPermitted( User user, PermissionObject permObj, PermissionScope pe
Session loginSystemUser();


String issueJWTToken( String environmentId, String containerId ) throws TokenCreateException;
String issueJWTToken( final String origin ) throws TokenCreateException;

boolean verifyJWTToken( String token ) throws TokenParseException;
boolean verifyJWTToken( final String token ) throws TokenParseException;

/* *************************************************
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.subutai.core.identity.api;


import java.util.Date;

import io.subutai.core.identity.api.exception.TokenParseException;


public interface TokenHelper
{
String getToken();

boolean verify( String secret );

String getSubject() throws TokenParseException;

Date getExpirationTime() throws TokenParseException;
}
Loading

0 comments on commit 89d6dc6

Please sign in to comment.