Search in sources :

Example 26 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class PutObjectAcl method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_OBJECT_ACL_START);
    String bucket = s3Parameter.getBucketName();
    String object = s3Parameter.getObjectName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    DataPutObjectAcl dataPutObjectAcl = new DataPutObjectAcl(s3Parameter);
    dataPutObjectAcl.extract();
    String versionId = dataPutObjectAcl.getVersionId();
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
    } else {
        objMeta = open(bucket, object, versionId);
    }
    objMeta.setAcl(GWUtils.makeOriginalXml(objMeta.getAcl(), s3Parameter));
    checkGrantObjectOwner(s3Parameter.isPublicAccess(), objMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE_ACP);
    accessControlPolicy = new AccessControlPolicy();
    accessControlPolicy.aclList = new AccessControlList();
    accessControlPolicy.aclList.grants = new ArrayList<Grant>();
    accessControlPolicy.owner = new Owner();
    accessControlPolicy.owner.id = String.valueOf(s3Parameter.getUser().getUserId());
    accessControlPolicy.owner.displayName = s3Parameter.getUser().getUserName();
    String xml = GWUtils.makeAclXml(accessControlPolicy, null, dataPutObjectAcl.hasAclKeyword(), dataPutObjectAcl.getAclXml(), dataPutObjectAcl.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataPutObjectAcl.getGrantRead(), dataPutObjectAcl.getGrantWrite(), dataPutObjectAcl.getGrantFullControl(), dataPutObjectAcl.getGrantReadAcp(), dataPutObjectAcl.getGrantWriteAcp(), s3Parameter);
    logger.debug(GWConstants.LOG_ACL, xml);
    objMeta.setAcl(xml);
    updateObjectAcl(objMeta);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : AccessControlList(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList) Grant(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant) AccessControlPolicy(com.pspace.ifs.ksan.gw.format.AccessControlPolicy) Owner(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) DataPutObjectAcl(com.pspace.ifs.ksan.gw.data.DataPutObjectAcl) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 27 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class PutObjectTagging method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_OBJECT_TAGGING_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
    String object = s3Parameter.getObjectName();
    DataPutObjectTagging dataPutObjectTagging = new DataPutObjectTagging(s3Parameter);
    dataPutObjectTagging.extract();
    String taggingCount = GWConstants.TAGGING_INIT;
    String taggingXml = dataPutObjectTagging.getTaggingXml();
    try {
        Tagging tagging = new XmlMapper().readValue(taggingXml, Tagging.class);
        // 중복 지우기 item이 10개 미만이기 때문에 for loop가 빠름
        if (tagging != null) {
            if (tagging.tagset != null && tagging.tagset.tags != null) {
                for (Tag t : tagging.tagset.tags) {
                    // key, value 길이 체크
                    if (t.key.length() > GWConstants.TAG_KEY_MAX) {
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                    if (t.value.length() > GWConstants.TAG_VALUE_MAX) {
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                }
            }
            if (tagging.tagset != null && tagging.tagset.tags != null) {
                if (tagging.tagset.tags.size() > GWConstants.TAG_MAX_SIZE) {
                    throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                }
                taggingCount = String.valueOf(tagging.tagset.tags.size());
            }
        }
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    String versionId = dataPutObjectTagging.getVersionId();
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
    } else {
        objMeta = open(bucket, object, versionId);
    }
    S3Metadata s3Metadata = null;
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        logger.debug(GWConstants.LOG_META, objMeta.getMeta());
        s3Metadata = objectMapper.readValue(objMeta.getMeta(), S3Metadata.class);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Metadata.setTaggingCount(taggingCount);
    ObjectMapper jsonMapper = new ObjectMapper();
    String jsonMeta = "";
    try {
        jsonMeta = jsonMapper.writeValueAsString(s3Metadata);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    objMeta.setMeta(jsonMeta);
    objMeta.setTag(taggingXml);
    updateObjectTagging(objMeta);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) DataPutObjectTagging(com.pspace.ifs.ksan.gw.data.DataPutObjectTagging) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) DataPutObjectTagging(com.pspace.ifs.ksan.gw.data.DataPutObjectTagging) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 28 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class ListObjectsV2 method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_LIST_OBJECT_V2_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    DataListObjectV2 dataListObjectV2 = new DataListObjectV2(s3Parameter);
    dataListObjectV2.extract();
    // read header
    S3ObjectList s3ObjectList = new S3ObjectList();
    if (!Strings.isNullOrEmpty(dataListObjectV2.getMaxKeys())) {
        if (Integer.valueOf(dataListObjectV2.getMaxKeys()) < 0) {
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
        s3ObjectList.setMaxKeys(dataListObjectV2.getMaxKeys());
    } else {
        s3ObjectList.setMaxKeys(GWConstants.DEFAULT_MAX_KEYS);
    }
    s3ObjectList.setContinuationToken(dataListObjectV2.getContinuationToken());
    s3ObjectList.setDelimiter(dataListObjectV2.getDelimiter());
    s3ObjectList.setEncodingType(dataListObjectV2.getEncodingType());
    s3ObjectList.setPrefix(dataListObjectV2.getPrefix());
    s3ObjectList.setStartAfter(dataListObjectV2.getStartAfter());
    s3ObjectList.setFetchOwner(dataListObjectV2.getFetchOwner());
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    ObjectListParameter objectListParameter = listObjectV2(bucket, s3ObjectList);
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.LIST_BUCKET_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_NAME, bucket);
        String encodingType = s3ObjectList.getEncodingType();
        String prefix = s3ObjectList.getPrefix();
        if (prefix == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(encodingType, prefix));
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_KEYS, String.valueOf(s3ObjectList.getMaxKeys()));
        if (s3ObjectList.getStartAfter() == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_START_AFTER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_START_AFTER, GWUtils.encodeObjectName(encodingType, s3ObjectList.getStartAfter()));
        }
        if (s3ObjectList.getContinuationToken() == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_CONTINUEATION_TOKEN);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_CONTINUEATION_TOKEN, GWUtils.encodeObjectName(encodingType, s3ObjectList.getContinuationToken()));
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_COUNT, String.valueOf(objectListParameter.getObjects().size()));
        if (s3ObjectList.getDelimiter() != null) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeObjectName(encodingType, s3ObjectList.getDelimiter()));
        }
        if (encodingType != null && encodingType.equals(GWConstants.URL)) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, encodingType);
        }
        if (objectListParameter.isTruncated()) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_CONTINUATION_TOKEN, GWUtils.encodeObjectName(encodingType, objectListParameter.getNextMarker()));
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
        }
        for (S3Metadata s3Metadata : objectListParameter.getObjects()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_CONTENTS);
            writeSimpleElement(xmlStreamWriter, GWConstants.KEY, GWUtils.encodeObjectName(encodingType, s3Metadata.getName()));
            if (s3Metadata.getLastModified() != null) {
                writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
            }
            if (s3Metadata.getETag() != null) {
                writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
            }
            if (!Strings.isNullOrEmpty(s3ObjectList.getFetchOwner()) && s3ObjectList.getFetchOwner().equals(GWConstants.XML_TRUE)) {
                writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
            }
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, s3Metadata.getContentLength().toString());
            writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, s3Metadata.getTier());
            xmlStreamWriter.writeEndElement();
        }
        for (Entry<String, String> entry : objectListParameter.getCommonPrefixes().entrySet()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_COMMON_PREFIXES);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(encodingType, entry.getValue()));
            xmlStreamWriter.writeEndElement();
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) IOException(java.io.IOException) S3ObjectList(com.pspace.ifs.ksan.gw.identity.S3ObjectList) DataListObjectV2(com.pspace.ifs.ksan.gw.data.DataListObjectV2) ObjectListParameter(com.pspace.ifs.ksan.gw.identity.ObjectListParameter) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Writer(java.io.Writer) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 29 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class PostObject method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_POST_OBJECT_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    DataPostObject dataPostObject = new DataPostObject(s3Parameter);
    dataPostObject.extract();
    String object = dataPostObject.getKey();
    s3Parameter.setObjectName(object);
    logger.debug(GWConstants.LOG_BUCKET_OBJECT, bucket, object);
    if (Strings.isNullOrEmpty(dataPostObject.getKey())) {
        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
    }
    logger.info("policy list : {}", dataPostObject.getPolicy());
    if (!Strings.isNullOrEmpty(dataPostObject.getPolicy())) {
        Decoder decoder = Base64.getDecoder();
        byte[] bytePostPolicy = decoder.decode(dataPostObject.getPolicy());
        String postPolicy = new String(bytePostPolicy);
        ObjectMapper jsonMapper = new ObjectMapper();
        PostPolicy postPolicyJson = null;
        try {
            postPolicyJson = jsonMapper.readValue(postPolicy, PostPolicy.class);
        } catch (JsonProcessingException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
        }
        Map<String, String> conditionMap = new HashMap<String, String>();
        if (postPolicyJson.conditions == null) {
            logger.info(GWErrorCode.BAD_REQUEST.getMessage());
            throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
        }
        if (postPolicyJson.conditions.size() == 0) {
            logger.info(GWErrorCode.BAD_REQUEST.getMessage());
            throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
        }
        for (Object o : postPolicyJson.conditions) {
            // check
            logger.info("conditions ==> className(" + o.getClass().getName() + ")");
            if (o.getClass().getName().equals("java.util.LinkedHashMap")) {
                @SuppressWarnings("unchecked") Map<String, String> policyMap = (HashMap<String, String>) o;
                for (Map.Entry<String, String> s : policyMap.entrySet()) {
                    logger.info("conditions ==> key(" + s.getKey() + "), value(" + s.getValue() + ")");
                    dataPostObject.checkPolicy(s.getKey(), s.getValue());
                    conditionMap.put(s.getKey().toLowerCase(), s.getValue());
                }
            } else if (o.getClass().getName().equals("java.util.ArrayList")) {
                @SuppressWarnings("unchecked") List<Object> policyList = (List<Object>) o;
                if (!((String) policyList.get(0)).equalsIgnoreCase("starts-with") && !((String) policyList.get(0)).equalsIgnoreCase("eq") && !((String) policyList.get(0)).equalsIgnoreCase("content-length-range")) {
                    throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                }
                if (((String) policyList.get(0)).equalsIgnoreCase("eq")) {
                    logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
                    dataPostObject.checkPolicy((String) policyList.get(1), (String) policyList.get(2));
                } else if (((String) policyList.get(0)).equalsIgnoreCase("starts-with")) {
                    logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
                    dataPostObject.checkPolityStarts((String) policyList.get(1), (String) policyList.get(2));
                } else if (((String) policyList.get(0)).equalsIgnoreCase("content-length-range")) {
                    if (policyList.size() != 3) {
                        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
                        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                    }
                    logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
                    if ((int) policyList.get(1) < 0) {
                        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
                        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                    }
                    if (dataPostObject.getPayload().length < (int) policyList.get(1)) {
                        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
                        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                    }
                    if ((int) policyList.get(2) < 0) {
                        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
                        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                    }
                    if (dataPostObject.getPayload().length > (int) policyList.get(2)) {
                        logger.info(GWErrorCode.BAD_REQUEST.getMessage());
                        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                    }
                }
            } else {
                logger.info(o.getClass().getName());
            }
        }
        if (Strings.isNullOrEmpty(postPolicyJson.expiration)) {
            logger.info(GWErrorCode.BAD_REQUEST.getMessage());
            throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
        } else {
            dataPostObject.setExpiration(postPolicyJson.getExpiration());
        }
        // bucket check
        if (Strings.isNullOrEmpty(conditionMap.get(GWConstants.CATEGORY_BUCKET))) {
            logger.info(GWErrorCode.ACCESS_DENIED.getMessage());
            throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
        }
    }
    if (!Strings.isNullOrEmpty(dataPostObject.getAccessKey())) {
        if (Strings.isNullOrEmpty(dataPostObject.getSignature())) {
            logger.info(GWErrorCode.BAD_REQUEST.getMessage());
            throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
        }
        // signing check
        S3Signing s3signing = new S3Signing(s3Parameter);
        s3Parameter = s3signing.validatePost(dataPostObject);
        if (!isGrantBucket(String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE)) {
            throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
        }
    } else {
        if (!isGrantBucket(GWConstants.LOG_REQUEST_ROOT_ID, GWConstants.GRANT_WRITE)) {
            throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
        }
    }
    s3Parameter.setInputStream(new ByteArrayInputStream(dataPostObject.getPayload()));
    String cacheControl = dataPostObject.getCacheControl();
    String contentDisposition = dataPostObject.getContentDisposition();
    String contentEncoding = dataPostObject.getContentEncoding();
    String contentLanguage = dataPostObject.getContentLanguage();
    String contentType = dataPostObject.getContentType();
    String customerAlgorithm = dataPostObject.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataPostObject.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataPostObject.getServerSideEncryptionCustomerKeyMD5();
    String serversideEncryption = dataPostObject.getServerSideEncryption();
    S3Metadata s3Metadata = new S3Metadata();
    s3Metadata.setOwnerId(Long.toString(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    s3Metadata.setUserMetadataMap(dataPostObject.getUserMetadata());
    if (!Strings.isNullOrEmpty(serversideEncryption)) {
        if (!serversideEncryption.equalsIgnoreCase(GWConstants.AES256)) {
            logger.error(GWErrorCode.NOT_IMPLEMENTED.getMessage() + GWConstants.SERVER_SIDE_OPTION);
            throw new GWException(GWErrorCode.NOT_IMPLEMENTED, s3Parameter);
        } else {
            s3Metadata.setServersideEncryption(serversideEncryption);
        }
    }
    if (!Strings.isNullOrEmpty(cacheControl)) {
        s3Metadata.setCacheControl(cacheControl);
    }
    if (!Strings.isNullOrEmpty(contentDisposition)) {
        s3Metadata.setContentDisposition(contentDisposition);
    }
    if (!Strings.isNullOrEmpty(contentEncoding)) {
        s3Metadata.setContentEncoding(contentEncoding);
    }
    if (!Strings.isNullOrEmpty(contentLanguage)) {
        s3Metadata.setContentLanguage(contentLanguage);
    }
    if (!Strings.isNullOrEmpty(contentType)) {
        s3Metadata.setContentType(contentType);
    }
    if (!Strings.isNullOrEmpty(customerAlgorithm)) {
        s3Metadata.setCustomerAlgorithm(customerAlgorithm);
    }
    if (!Strings.isNullOrEmpty(customerKey)) {
        s3Metadata.setCustomerKey(customerKey);
    }
    if (!Strings.isNullOrEmpty(customerKeyMD5)) {
        s3Metadata.setCustomerKeyMD5(customerKeyMD5);
    }
    String aclXml = GWUtils.makeAclXml(accessControlPolicy, null, dataPostObject.getAclKeyword(), null, dataPostObject.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataPostObject.getGrantRead(), dataPostObject.getGrantWrite(), dataPostObject.getGrantFullControl(), dataPostObject.getGrantReadAcp(), dataPostObject.getGrantWriteAcp(), s3Parameter);
    String bucketEncryption = getBucketInfo().getEncryption();
    S3ServerSideEncryption encryption = new S3ServerSideEncryption(bucketEncryption, serversideEncryption, customerAlgorithm, customerKey, customerKeyMD5, s3Parameter);
    encryption.build();
    // Tagging information
    String taggingCount = GWConstants.TAGGING_INIT;
    String taggingxml = "";
    Tagging tagging = new Tagging();
    tagging.tagset = new TagSet();
    try {
        if (dataPostObject.getTagging() != null)
            tagging = new XmlMapper().readValue(dataPostObject.getTagging(), Tagging.class);
    } catch (JsonProcessingException e) {
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    try {
        if (tagging != null)
            taggingxml = new XmlMapper().writeValueAsString(tagging);
    } catch (JsonProcessingException e) {
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    if (tagging != null) {
        if (tagging.tagset != null && tagging.tagset.tags != null) {
            for (Tag t : tagging.tagset.tags) {
                // key, value 길이 체크
                if (t.key.length() > 128) {
                    throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                }
                if (t.value.length() > 256) {
                    throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                }
            }
        }
        if (tagging.tagset != null && tagging.tagset.tags != null) {
            if (tagging.tagset.tags.size() > 10) {
                throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
            }
            taggingCount = String.valueOf(tagging.tagset.tags.size());
        }
    }
    String versioningStatus = getBucketVersioning(bucket);
    String versionId = null;
    Metadata objMeta = null;
    try {
        // check exist object
        objMeta = open(bucket, object);
        if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
            versionId = String.valueOf(System.nanoTime());
        } else {
            versionId = GWConstants.VERSIONING_DISABLE_TAIL;
        }
    } catch (GWException e) {
        logger.info(e.getMessage());
        if (GWConfig.getReplicaCount() > 1) {
            objMeta = create(bucket, object);
        } else {
            objMeta = createLocal(bucket, object);
        }
        if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
            versionId = String.valueOf(System.nanoTime());
        } else {
            versionId = GWConstants.VERSIONING_DISABLE_TAIL;
        }
    }
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, versionId, encryption);
    S3Object s3Object = objectOperation.putObject();
    s3Metadata.setETag(s3Object.getEtag());
    s3Metadata.setSize(s3Object.getFileSize());
    s3Metadata.setContentLength(s3Object.getFileSize());
    s3Metadata.setTier(GWConstants.AWS_TIER_STANTARD);
    s3Metadata.setLastModified(s3Object.getLastModified());
    s3Metadata.setDeleteMarker(s3Object.getDeleteMarker());
    s3Metadata.setVersionId(s3Object.getVersionId());
    s3Metadata.setTaggingCount(taggingCount);
    if (encryption.isEnableSSEServer()) {
        s3Metadata.setServersideEncryption(GWConstants.AES256);
    }
    s3Parameter.setFileSize(s3Object.getFileSize());
    ObjectMapper jsonMapper = new ObjectMapper();
    String jsonmeta = "";
    try {
        jsonmeta = jsonMapper.writeValueAsString(s3Metadata);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    logger.debug(GWConstants.LOG_PUT_OBJECT_PRIMARY_DISK_ID, objMeta.getPrimaryDisk().getId());
    try {
        objMeta.set(s3Object.getEtag(), taggingxml, jsonmeta, aclXml, s3Object.getFileSize());
        objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_FILE, true);
        insertObject(bucket, object, objMeta);
        logger.debug(GWConstants.LOG_PUT_OBJECT_INFO, bucket, object, s3Object.getFileSize(), s3Object.getEtag(), aclXml, versionId);
    } catch (GWException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Parameter.getResponse().addHeader(HttpHeaders.ETAG, GWUtils.maybeQuoteETag(s3Object.getEtag()));
    if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
        s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, s3Object.getVersionId());
        logger.debug(GWConstants.LOG_PUT_OBJECT_VERSIONID, s3Object.getVersionId());
    }
    if (!Strings.isNullOrEmpty(dataPostObject.getSuccessActionRedirect())) {
        try {
            s3Parameter.getResponse().sendRedirect(dataPostObject.getSuccessActionRedirect() + GWConstants.PARAMETER_BUCKET + bucket + GWConstants.PARAMETER_KEY + s3Parameter.getObjectName() + GWConstants.PARAMETER_ETAG + s3Metadata.getETag() + GWConstants.ENCODING_DOUBLE_QUOTE);
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
        }
        dataPostObject.setSuccessActionStatus(GWConstants.STATUS_SC_OK);
    }
    if (!Strings.isNullOrEmpty(dataPostObject.getSuccessActionStatus())) {
        switch(Integer.parseInt(dataPostObject.getSuccessActionStatus())) {
            case HttpServletResponse.SC_OK:
                s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
                break;
            case HttpServletResponse.SC_CREATED:
                s3Parameter.getResponse().setStatus(HttpServletResponse.SC_CREATED);
                break;
            case HttpServletResponse.SC_NO_CONTENT:
                s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
                break;
            default:
                s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
                break;
        }
    } else {
        s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}
Also used : HashMap(java.util.HashMap) S3ServerSideEncryption(com.pspace.ifs.ksan.gw.object.S3ServerSideEncryption) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) Decoder(java.util.Base64.Decoder) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) TagSet(com.pspace.ifs.ksan.gw.format.Tagging.TagSet) S3Signing(com.pspace.ifs.ksan.gw.sign.S3Signing) List(java.util.List) GWException(com.pspace.ifs.ksan.gw.exception.GWException) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) DataPostObject(com.pspace.ifs.ksan.gw.data.DataPostObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) ByteArrayInputStream(java.io.ByteArrayInputStream) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) DataPostObject(com.pspace.ifs.ksan.gw.data.DataPostObject) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag) HashMap(java.util.HashMap) Map(java.util.Map) PostPolicy(com.pspace.ifs.ksan.gw.format.PostPolicy)

Example 30 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class PutBucketAcl method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_ACL_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucketOwner(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE_ACP);
    DataPutBucketAcl dataPutBucketAcl = new DataPutBucketAcl(s3Parameter);
    dataPutBucketAcl.extract();
    AccessControlPolicy preAccessControlPolicy = null;
    try {
        XmlMapper xmlMapper = new XmlMapper();
        preAccessControlPolicy = xmlMapper.readValue(getBucketInfo().getAcl(), AccessControlPolicy.class);
    } catch (JsonMappingException e) {
        logger.error(e.getMessage());
        new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    } catch (JsonProcessingException e) {
        logger.error(e.getMessage());
        new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    accessControlPolicy = new AccessControlPolicy();
    accessControlPolicy.aclList = new AccessControlList();
    accessControlPolicy.aclList.grants = new ArrayList<Grant>();
    accessControlPolicy.owner = new Owner();
    accessControlPolicy.owner.id = String.valueOf(s3Parameter.getUser().getUserId());
    accessControlPolicy.owner.displayName = s3Parameter.getUser().getUserName();
    String xml = GWUtils.makeAclXml(accessControlPolicy, preAccessControlPolicy, dataPutBucketAcl.hasAclKeyword(), dataPutBucketAcl.getAclXml(), dataPutBucketAcl.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataPutBucketAcl.getGrantRead(), dataPutBucketAcl.getGrantWrite(), dataPutBucketAcl.getGrantFullControl(), dataPutBucketAcl.getGrantReadAcp(), dataPutBucketAcl.getGrantWriteAcp(), s3Parameter);
    logger.debug(GWConstants.LOG_ACL, xml);
    updateBucketAcl(bucket, xml);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : AccessControlList(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList) DataPutBucketAcl(com.pspace.ifs.ksan.gw.data.DataPutBucketAcl) Grant(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant) AccessControlPolicy(com.pspace.ifs.ksan.gw.format.AccessControlPolicy) Owner(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

GWException (com.pspace.ifs.ksan.gw.exception.GWException)57 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)57 IOException (java.io.IOException)31 Metadata (com.pspace.ifs.ksan.objmanager.Metadata)17 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)16 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLStreamException (javax.xml.stream.XMLStreamException)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Writer (java.io.Writer)12 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)12 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)10 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)9 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)7 S3Object (com.pspace.ifs.ksan.gw.object.S3Object)6 UnknownHostException (java.net.UnknownHostException)6 AccessControlPolicy (com.pspace.ifs.ksan.gw.format.AccessControlPolicy)5 AccessControlList (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList)5 Grant (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant)5 Owner (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner)5