Skip to content

Commit

Permalink
Merge pull request #475 from devicehive/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
tmatvienko authored Jan 19, 2018
2 parents 2d021a2 + 33e804a commit bddfe89
Show file tree
Hide file tree
Showing 206 changed files with 6,503 additions and 2,199 deletions.
231 changes: 155 additions & 76 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ properties([
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '7', numToKeepStr: '7'))
])

def publishable_branches = ["development", "master"]
def deployable_branches = ["development"]
def test_branches = ["development", "master"]
def publish_branches = ["development", "master"]
def deploy_branches = ["development"]

node('docker') {
stage('Build jars') {
stage('Build jars') {
node('docker') {
echo 'Building jars ...'
def maven = docker.image('maven:3.5.2-jdk-8')
maven.pull()
maven.inside {
checkout scm
sh 'mvn clean package -DskipTests'
sh 'mvn test'
archiveArtifacts artifacts: 'devicehive-backend/target/devicehive-backend-*-boot.jar, devicehive-auth/target/devicehive-auth-*-boot.jar, devicehive-plugin/target/devicehive-plugin-*-boot.jar, devicehive-frontend/target/devicehive-frontend-*-boot.jar, devicehive-common/target/devicehive-common-*-shade.jar', fingerprint: true, onlyIfSuccessful: true

stash includes:'devicehive-backend/target/devicehive-backend-*-boot.jar, devicehive-auth/target/devicehive-auth-*-boot.jar, devicehive-plugin/target/devicehive-plugin-*-boot.jar, devicehive-frontend/target/devicehive-frontend-*-boot.jar, devicehive-common/target/devicehive-common-*-shade.jar', name: 'jars'
try {
sh 'mvn test'
}
finally {
junit '**/target/surefire-reports/TEST-*.xml'
}
def artifacts = 'devicehive-backend/target/devicehive-backend-*-boot.jar, devicehive-auth/target/devicehive-auth-*-boot.jar, devicehive-plugin/target/devicehive-plugin-*-boot.jar, devicehive-frontend/target/devicehive-frontend-*-boot.jar, devicehive-common/target/devicehive-common-*-shade.jar'
archiveArtifacts artifacts: artifacts, fingerprint: true, onlyIfSuccessful: true
stash includes: artifacts, name: 'jars'
}
}
}

stage('Build and publish Docker images in CI repository') {
stage('Build and publish Docker images in CI repository') {
node('docker') {
echo 'Building images ...'
unstash 'jars'
def auth = docker.build('devicehiveci/devicehive-auth:${BRANCH_NAME}', '-f dockerfiles/devicehive-auth.Dockerfile .')
def auth = docker.build('devicehiveci/devicehive-auth:${BRANCH_NAME}', '--pull -f dockerfiles/devicehive-auth.Dockerfile .')
def plugin = docker.build('devicehiveci/devicehive-plugin:${BRANCH_NAME}', '-f dockerfiles/devicehive-plugin.Dockerfile .')
def frontend = docker.build('devicehiveci/devicehive-frontend:${BRANCH_NAME}', '-f dockerfiles/devicehive-frontend.Dockerfile .')
def backend = docker.build('devicehiveci/devicehive-backend:${BRANCH_NAME}', '-f dockerfiles/devicehive-backend.Dockerfile .')
Expand All @@ -40,78 +48,66 @@ node('docker') {
}
}

if (publishable_branches.contains(env.BRANCH_NAME)) {
stage('Run regression tests'){
node('tests-runner'){
try {
dir('devicehive-docker'){
echo("Clone Docker Compose files")
git branch: 'development', url: 'https://github.com/devicehive/devicehive-docker.git', depth: 1
}

dir('devicehive-docker/rdbms-image'){
writeFile file: '.env', text: """COMPOSE_FILE=docker-compose.yml:ci-images.yml
DH_TAG=${BRANCH_NAME}
JWT_SECRET=devicehive
"""

echo("Start DeviceHive")
sh '''
sudo docker-compose pull
sudo docker-compose up -d
'''
}

echo("Wait for devicehive")
timeout(time:5, unit: 'MINUTES') {
waitUntil{
def fe_status = sh script: 'curl --output /dev/null --silent --head --fail "http://127.0.0.1/api/rest/info"', returnStatus: true
return (fe_status == 0)
if (test_branches.contains(env.BRANCH_NAME)) {
stage('Run integration tests'){
parallel rpc: {
stage('Run integration tests with rpc'){
node('tests-runner'){
try {
clone_devicehive_docker()
dir('devicehive-docker/rdbms-image'){
writeFile file: '.env', text: """COMPOSE_PROJECT_NAME=ci
COMPOSE_FILE=docker-compose.yml:ci-images.yml
DH_TAG=${BRANCH_NAME}
JWT_SECRET=devicehive
DH_FE_SPRING_PROFILES_ACTIVE=rpc-client
DH_BE_SPRING_PROFILES_ACTIVE=rpc-server
"""

start_devicehive()
}
wait_for_devicehive_is_up()
run_devicehive_tests()
} finally {
archive_container_logs('rpc')
zip archive: true, dir: 'devicehive-tests', glob: 'mochawesome-report/**', zipFile: 'mochawesome-report.zip'
shutdown_devicehive()
cleanWs()
}
}

dir('devicehive-tests') {
echo("Clone regression tests")
git branch: 'development', url: 'https://github.com/devicehive/devicehive-tests.git', depth: 1

echo("Install dependencies with npm")
sh '''
sudo npm install -g mocha mochawesome
sudo npm i
'''

echo("Configure tests")
sh '''
cp config.json config.json.orig
cat config.json.orig | \\
jq ".server.wsUrl = \\"ws://127.0.0.1/api/websocket\\"" | \\
jq ".server.ip = \\"127.0.0.1\\"" | \\
jq ".server.port = \\"80\\"" | \\
jq ".server.restUrl = \\"http://127.0.0.1/api/rest\\"" | \\
jq ".server.authRestUrl = \\"http://127.0.0.1/auth/rest\\"" > config.json
'''

timeout(time:10, unit: 'MINUTES') {
echo("Run integration tests")
sh 'mocha --exit -R mochawesome integration-tests'
}
},
wsproxy: {
stage('Run integration tests with ws-proxy'){
node('tests-runner'){
try {
clone_devicehive_docker()
dir('devicehive-docker/rdbms-image'){
writeFile file: '.env', text: """COMPOSE_PROJECT_NAME=ci
COMPOSE_FILE=docker-compose.yml:ci-images.yml
DH_TAG=${BRANCH_NAME}
JWT_SECRET=devicehive
"""

start_devicehive()
}
wait_for_devicehive_is_up()
run_devicehive_tests()
} finally {
archive_container_logs('ws-proxy')
zip archive: true, dir: 'devicehive-tests', glob: 'mochawesome-report/**', zipFile: 'mochawesome-report.zip'
shutdown_devicehive()
cleanWs()
}
}
} finally {
zip archive: true, dir: 'devicehive-tests', glob: 'mochawesome-report/**', zipFile: 'mochawesome-report.zip'
dir('devicehive-docker/rdbms-image') {
sh '''
sudo docker-compose kill
sudo docker-compose down
sudo docker volume ls -qf dangling=true | xargs -r sudo docker volume rm
'''
}
cleanWs()
}
}
}
}

node('docker') {
stage('Publish image in main repository') {
if (publish_branches.contains(env.BRANCH_NAME)) {
stage('Publish image in main repository') {
node('docker') {
// Builds from 'master' branch will have 'latest' tag
def IMAGE_TAG = (env.BRANCH_NAME == 'master') ? 'latest' : env.BRANCH_NAME

Expand All @@ -134,17 +130,100 @@ if (publishable_branches.contains(env.BRANCH_NAME)) {
}
}

if (deployable_branches.contains(env.BRANCH_NAME)) {
if (deploy_branches.contains(env.BRANCH_NAME)) {
stage('Deploy build to dev server'){
node('dev-server') {
dir('/home/centos/devicehive-docker/rdbms-image'){
sh '''
sed -i -e "s/DH_TAG=.*/DH_TAG=${BRANCH_NAME}/g" .env
sudo docker-compose pull
sudo docker-compose up -d
echo "$(date): Deployed build from ${BRANCH_NAME} to dev server" > ./jenkins-cd.timestamp
echo "$(date): Successfully deployed build #${BUILD_NUMBER} from ${BRANCH_NAME} branch" > ./jenkins-cd.timestamp
'''
}
}
}
}

def clone_devicehive_docker(){
dir('devicehive-docker'){
echo("Clone Docker Compose files")
git branch: 'development', url: 'https://github.com/devicehive/devicehive-docker.git', depth: 1
}
}

def start_devicehive() {
echo("Start DeviceHive")
sh '''
sudo docker-compose pull
sudo docker-compose up -d
'''
}

def wait_for_devicehive_is_up() {
echo("Wait for devicehive")
timeout(time:5, unit: 'MINUTES') {
waitUntil{
def is_up = sh script: 'curl --output /dev/null --silent --head --fail "http://127.0.0.1/api/rest/info"', returnStatus: true
return (is_up == 0)
}
}
}

def run_devicehive_tests() {
dir('devicehive-tests') {
echo("Clone integration tests")
git branch: 'development', url: 'https://github.com/devicehive/devicehive-tests.git', depth: 1

echo("Install dependencies with npm")
sh '''
sudo npm install -g mocha mochawesome
sudo npm i
'''

echo("Configure tests")
sh '''
cp config.json config.json.orig
cat config.json.orig | \\
jq ".server.wsUrl = \\"ws://127.0.0.1/api/websocket\\"" | \\
jq ".server.ip = \\"127.0.0.1\\"" | \\
jq ".server.port = \\"80\\"" | \\
jq ".server.restUrl = \\"http://127.0.0.1/api/rest\\"" | \\
jq ".server.authRestUrl = \\"http://127.0.0.1/auth/rest\\"" > config.json
'''

timeout(time:10, unit: 'MINUTES') {
echo("Run integration tests")
sh 'mocha --exit -R mochawesome integration-tests'
}
}
}

def archive_container_logs(flavour) {
def logsdir = "${flavour}-container-logs"
sh """
mkdir ${logsdir} || :
sudo docker logs ci_dh_auth_1 > ${logsdir}/auth.log 2>&1
sudo docker logs ci_dh_backend_1 > ${logsdir}/backend.log 2>&1
sudo docker logs ci_dh_frontend_1 > ${logsdir}/frontend.log 2>&1
sudo docker logs ci_dh_proxy_1 > ${logsdir}/proxy.log 2>&1
sudo docker logs ci_hazelcast_1 > ${logsdir}/hazelcast.log 2>&1
sudo docker logs ci_kafka_1 > ${logsdir}/kafka.log 2>&1
sudo docker logs ci_wsproxy_1 > ${logsdir}/wsproxy.log 2>&1
sudo docker logs ci_postgres_1 > ${logsdir}/postgres.log 2>&1
sudo docker logs ci_zookeeper_1 > ${logsdir}/zookeeper.log 2>&1
"""
def logs = "${logsdir}/auth.log, ${logsdir}/backend.log, ${logsdir}/frontend.log, ${logsdir}/proxy.log, ${logsdir}/hazelcast.log, ${logsdir}/kafka.log, ${logsdir}/wsproxy.log, ${logsdir}/postgres.log, ${logsdir}/zookeeper.log"
archiveArtifacts artifacts: logs, fingerprint: true
}

def shutdown_devicehive() {
echo("Shutting down DeviceHive instance and cleaning up")
dir('devicehive-docker/rdbms-image') {
sh '''
sudo docker-compose kill
sudo docker-compose down
sudo docker volume ls -qf dangling=true | xargs -r sudo docker volume rm
'''
}
}
2 changes: 1 addition & 1 deletion devicehive-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>devicehive-server</artifactId>
<groupId>com.devicehive</groupId>
<version>3.4.2</version>
<version>3.4.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import java.io.IOException;
import java.net.URL;

import static com.devicehive.configuration.Constants.X_FORWARDED_PORT_HEADER_NAME;
import static com.devicehive.configuration.Constants.X_FORWARDED_PROTO_HEADER_NAME;

@WebFilter("/swagger")
public class SwaggerFilter extends OncePerRequestFilter {

Expand All @@ -49,6 +52,17 @@ public class SwaggerFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

final String xForwardedProto = request.getHeader(X_FORWARDED_PROTO_HEADER_NAME);
final String xForwardedPort = request.getHeader(X_FORWARDED_PORT_HEADER_NAME);

if (xForwardedProto != null) {
swaggerProtocol = xForwardedProto;
}

if (xForwardedPort != null) {
swaggerPort = xForwardedPort;
}

logger.debug("swagger.protocol: {}", swaggerProtocol);
logger.debug("swagger.port: {}", swaggerPort);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ private Response getRefreshResponse(JwtPluginPayload payload) {
return ResponseFactory.response(UNAUTHORIZED,
new ErrorResponse(UNAUTHORIZED.getStatusCode(), PLUGIN_NOT_FOUND));
}
if (!pluginVO.getStatus().equals(PluginStatus.ACTIVE)) {
logger.warn(PLUGIN_NOT_ACTIVE);
return ResponseFactory.response(UNAUTHORIZED,
new ErrorResponse(UNAUTHORIZED.getStatusCode(), PLUGIN_NOT_ACTIVE));
}

JwtTokenVO responseTokenVO = new JwtTokenVO();
responseTokenVO.setAccessToken(tokenService.generateJwtAccessToken(payload, false));
Expand Down Expand Up @@ -245,11 +240,6 @@ public Response authenticatePlugin(String jwtPluginToken) {
return ResponseFactory.response(UNAUTHORIZED,
new ErrorResponse(UNAUTHORIZED.getStatusCode(), PLUGIN_NOT_FOUND));
}
if (!PluginStatus.ACTIVE.equals(pluginVO.getStatus())) {
logger.warn(PLUGIN_NOT_ACTIVE);
return ResponseFactory.response(UNAUTHORIZED,
new ErrorResponse(UNAUTHORIZED.getStatusCode(), PLUGIN_NOT_ACTIVE));
}
}

return ResponseFactory.response(OK, jwtPluginPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
import com.devicehive.security.jwt.JwtUserPayload;
import com.devicehive.service.BaseUserService;
import com.devicehive.util.HiveValidator;
import com.devicehive.vo.JwtRequestVO;
import com.devicehive.vo.JwtTokenVO;
import com.devicehive.vo.NetworkVO;
import com.devicehive.vo.UserVO;
import com.devicehive.vo.UserWithNetworkVO;
import com.devicehive.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -75,14 +71,15 @@ public JwtTokenVO createJwtToken(@NotNull final JwtRequestVO request) {
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public JwtTokenVO createJwtToken(@NotNull UserVO user) {
Set<String> networkIds = new HashSet<>();
Set<String> deviceIds = new HashSet<>();
Set<String> deviceTypeIds = new HashSet<>();
Set<Integer> actions = new HashSet<>();
if (user.isAdmin()) {
networkIds.add("*");
deviceIds.add("*");
deviceTypeIds.add("*");
actions.add(ANY.getId());
} else {
UserWithNetworkVO userWithNetwork = userService.findUserWithNetworks(user.getId());
UserWithDeviceTypeVO userWithDeviceType = userService.findUserWithDeviceType(user.getId());
// TODO: check if needed
userService.refreshUserLoginData(user);

Expand All @@ -91,7 +88,16 @@ public JwtTokenVO createJwtToken(@NotNull UserVO user) {
networks.forEach(network -> {
networkIds.add(network.getId().toString());
});
deviceIds.add("*");
}
if (userWithDeviceType.getAllDeviceTypesAvailable()) {
deviceTypeIds.add("*");
} else {
Set<DeviceTypeVO> deviceTypes = userWithDeviceType.getDeviceTypes();
if (!deviceTypes.isEmpty()) {
deviceTypes.forEach(deviceType -> {
deviceTypeIds.add(deviceType.getId().toString());
});
}
}
actions = getIdSet(getClientHiveActions());
}
Expand All @@ -101,7 +107,7 @@ public JwtTokenVO createJwtToken(@NotNull UserVO user) {
.withUserId(user.getId())
.withActions(actions)
.withNetworkIds(networkIds)
.withDeviceIds(deviceIds)
.withDeviceTypeIds(deviceTypeIds)
.buildPayload();

JwtUserPayload refreshPayload = JwtUserPayload.newBuilder().withPayload(accessPayload)
Expand Down
Loading

0 comments on commit bddfe89

Please sign in to comment.