Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat(audit): first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGeraud committed Nov 14, 2017
1 parent e2caf33 commit 347b9c4
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.AuditRepository;
import io.gravitee.repository.management.api.ViewRepository;
import io.gravitee.repository.management.api.search.AuditCriteria;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.management.model.Audit;
import io.gravitee.repository.management.model.View;
import io.gravitee.repository.mongodb.management.internal.api.ViewMongoRepository;
import io.gravitee.repository.mongodb.management.internal.audit.AuditMongoRepository;
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo;
import io.gravitee.repository.mongodb.management.internal.model.ViewMongo;
import io.gravitee.repository.mongodb.management.mapper.GraviteeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
public class MongoAuditRepository implements AuditRepository {

private final Logger LOGGER = LoggerFactory.getLogger(MongoAuditRepository.class);

@Autowired
private AuditMongoRepository internalAuditRepo;

@Autowired
private GraviteeMapper mapper;

@Override
public Page<Audit> search(AuditCriteria filter, Pageable pageable) {
Page<AuditMongo> auditsMongo = internalAuditRepo.search(filter, pageable);

List<Audit> content = mapper.collection2list(auditsMongo.getContent(), AuditMongo.class, Audit.class);
return new Page<>(content, auditsMongo.getPageNumber(), (int) auditsMongo.getPageElements(), auditsMongo.getTotalElements());
}

@Override
public Optional<Audit> findById(String id) throws TechnicalException {
LOGGER.debug("Find view by ID [{}]", id);

final AuditMongo audit = internalAuditRepo.findOne(id);

LOGGER.debug("Find view by ID [{}] - Done", id);
return Optional.ofNullable(mapper.map(audit, Audit.class));
}

@Override
public Audit create(Audit audit) throws TechnicalException {
LOGGER.debug("Create audit {}", audit.toString());

AuditMongo auditMongo = mapper.map(audit, AuditMongo.class);
AuditMongo createdAuditMongo = internalAuditRepo.insert(auditMongo);

Audit res = mapper.map(createdAuditMongo, Audit.class);

LOGGER.debug("Create view [{}] - Done", audit.toString());

return res;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.audit;

import io.gravitee.repository.mongodb.management.internal.model.AuditMongo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Repository
public interface AuditMongoRepository extends MongoRepository<AuditMongo, String>, AuditMongoRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.audit;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.api.search.AuditCriteria;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
public interface AuditMongoRepositoryCustom {

Page<AuditMongo> search(AuditCriteria filter, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.audit;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.api.search.AuditCriteria;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.mongodb.management.internal.model.AuditMongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
public class AuditMongoRepositoryImpl implements AuditMongoRepositoryCustom {

@Autowired
private MongoTemplate mongoTemplate;

@Override
public Page<AuditMongo> search(AuditCriteria filter, Pageable pageable) {
Query query = new Query();

if (filter.getReferences() != null && !filter.getReferences().isEmpty()) {
filter.getReferences().forEach(
(referenceType, referenceIds) -> query.addCriteria(
where("referenceType").is(referenceType).
andOperator(
where("referenceId").in(referenceIds))));
}

if (filter.getFrom() != 0 && filter.getTo() != 0) {
query.addCriteria(
where("createdAt").gte(new Date(filter.getFrom())).
andOperator(
where("createdAt").lte(new Date(filter.getTo()))));
} else {
if (filter.getFrom() != 0) {
query.addCriteria(where("createdAt").gte(new Date(filter.getFrom())));
}
if (filter.getTo() != 0) {
query.addCriteria(where("createdAt").lte(new Date(filter.getTo())));
}
}

if (filter.getEvents() != null && !filter.getEvents().isEmpty()) {
query.addCriteria(where("event").in(filter.getEvents()));
}

query.with(new PageRequest(
pageable.pageNumber() - 1 < 0 ? 0 : pageable.pageNumber() - 1,
pageable.pageSize(),
new Sort(Sort.Direction.DESC, "createdAt")));

List<AuditMongo> audits = mongoTemplate.find(query, AuditMongo.class);
long total = mongoTemplate.count(query, AuditMongo.class);

return new Page<>(audits, pageable.pageNumber(), pageable.pageSize(), total);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.mongodb.management.internal.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Document(collection = "audits")
public class AuditMongo extends Auditable {

@Id
private String id;
private String referenceId;
private String referenceType;
private String username;
private String event;
private Map<String,String> properties;
private String patch;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getReferenceId() {
return referenceId;
}

public void setReferenceId(String referenceId) {
this.referenceId = referenceId;
}

public String getReferenceType() {
return referenceType;
}

public void setReferenceType(String referenceType) {
this.referenceType = referenceType;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getEvent() {
return event;
}

public void setEvent(String event) {
this.event = event;
}

public Map<String,String> getProperties() {
return properties;
}

public void setProperties(Map<String,String> properties) {
this.properties = properties;
}

public String getPatch() {
return patch;
}

public void setPatch(String patch) {
this.patch = patch;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuditMongo)) return false;
AuditMongo auditMongo = (AuditMongo) o;
return Objects.equals(id, auditMongo.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/scripts/create-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ db.memberships.createIndex( {"_id.referenceType":1, "_id.userId":1} );
db.memberships.createIndex( {"_id.referenceType":1, "_id.userId":1, "type":1} );
db.memberships.createIndex( {"_id.referenceType":1, "_id.referenceId":1, "roles":1} );
db.roles.createIndex( {"_id.scope": 1 } );
db.audits.createIndex( { "referenceType": 1, "referenceId": 1 } );
db.audits.createIndex( { "createdAt": 1 } );
db.rating.createIndex( { "api" : 1 } );

db.apis.reIndex();
Expand All @@ -30,4 +32,5 @@ db.keys.reIndex();
db.pages.reIndex();
db.memberships.reIndex();
db.roles.reIndex();
db.audits.reIndex();
db.rating.reIndex();

0 comments on commit 347b9c4

Please sign in to comment.