com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException Java Examples

The following examples show how to use com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatedSchemaMetadataInfo authorizeGetAggregatedSchemaInfo(UserAndGroups userAndGroups,
                                                                     AggregatedSchemaMetadataInfo aggregatedSchemaMetadataInfo)
        throws AuthorizationException, SchemaNotFoundException {
    SchemaMetadata sm = aggregatedSchemaMetadataInfo.getSchemaMetadata();

    authorizeSchemaMetadata(userAndGroups, sm, AccessType.READ);

    Collection<AggregatedSchemaBranch> filteredBranches =
            removeUnauthorizedAndNullEntities(aggregatedSchemaMetadataInfo.getSchemaBranches(),
                    aggregatedBranch -> authorizeGetAggregatedBranch(sm, userAndGroups, aggregatedBranch));

    Collection<SerDesInfo> serDesInfos = aggregatedSchemaMetadataInfo.getSerDesInfos();

    if(serDesInfos != null &&
            !serDesInfos.isEmpty() &&
            !authorizer.authorize(new Authorizer.SerdeResource(), AccessType.READ, userAndGroups)) {
        serDesInfos = new ArrayList<>();
    }

    return new AggregatedSchemaMetadataInfo(sm,
            aggregatedSchemaMetadataInfo.getId(),
            aggregatedSchemaMetadataInfo.getTimestamp(),
            filteredBranches,
            serDesInfos);
}
 
Example #2
Source File: SchemaRegistryResource.java    From registry with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/schemas/{name}/branches")
@ApiOperation(value = "Get list of registered schema branches",
        response = SchemaBranch.class, responseContainer = "List",
        tags = OPERATION_GROUP_OTHER)
@Timed
@UnitOfWork
public Response getAllBranches(@ApiParam(value = "Details about schema name",required = true) @PathParam("name") String schemaName,
                               @Context UriInfo uriInfo,
                               @Context SecurityContext securityContext) {
    try {
        Collection<SchemaBranch> schemaBranches = authorizationAgent.authorizeGetAllBranches(AuthorizationUtils.getUserAndGroups(securityContext),
                schemaRegistry, schemaName, schemaRegistry.getSchemaBranches(schemaName));
        return WSUtils.respondEntities(schemaBranches, Response.Status.OK);
    }  catch(SchemaNotFoundException e) {
        return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaName);
    } catch (Exception ex) {
        LOG.error("Encountered error while listing schema branches", ex);
        return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
    }
}
 
Example #3
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@Test
public void mergeSchemaWithDefaultMergeStrategy() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.toString(), SchemaCompatibility.NONE);

    SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
    SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
    SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
    SchemaIdVersion schemaBranch1Version2 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");

    schemaRegistryClient.mergeSchemaVersion(schemaBranch1Version2.getSchemaVersionId());

    Collection<SchemaVersionInfo> branchSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
    Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
    Assert.assertTrue(masterSchemaVersionInfos.size() == 2);
    Assert.assertTrue(branchSchemaVersionInfos.size() == 3);

    Long branchVersionsInInitiatedState = branchSchemaVersionInfos.stream().filter(
            schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
    Long masterVersionsInEnabledState = masterSchemaVersionInfos.stream().filter(
            schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
    Assert.assertTrue(branchVersionsInInitiatedState == 2);
    Assert.assertTrue(masterVersionsInEnabledState == 2);
}
 
Example #4
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
private List<SchemaVersionInfo> getSortedSchemaVersions(Long schemaBranchId) throws SchemaNotFoundException, SchemaBranchNotFoundException {
    List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
    schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranchId
            .toString()));
    List<OrderByField> orderByFields = new ArrayList<>();
    orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
    List<SchemaVersionInfo> schemaVersionInfos = new ArrayList<>();

    Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
    if (storables == null || storables.size() == 0) {
        if (schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId))
                             .getName()
                             .equals(SchemaBranch.MASTER_BRANCH))
            return Collections.emptyList();
        else
            throw new InvalidSchemaBranchVersionMapping(String.format("No schema versions are attached to the schema branch id : '%s'", schemaBranchId));
    }

    for (SchemaBranchVersionMapping storable : storables) {
        SchemaIdVersion schemaIdVersion = new SchemaIdVersion(storable.getSchemaVersionInfoId());
        schemaVersionInfos.add(schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion)));
    }

    return schemaVersionInfos;
}
 
Example #5
Source File: CustomStateTransitionTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransitionToRejectedState() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = createSchemaMetadata("test", SchemaCompatibility.NONE);
    SchemaBranch branch1 = new SchemaBranch("BRANCH1", schemaMetadata.getName());

    String schema1 = getSchema("/device.avsc");
    schemaRegistryClient.addSchemaMetadata(schemaMetadata);
    SchemaIdVersion schemaIdVersion1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version"));

    schemaRegistryClient.createSchemaBranch(schemaIdVersion1.getSchemaVersionId(), branch1);

    String schema2 = AvroSchemaRegistryClientUtil.getSchema("/device1.avsc");
    SchemaIdVersion schemaIdVersion2 = schemaRegistryClient.addSchemaVersion(branch1.getName(), schemaMetadata, new SchemaVersion(schema2, "modify version"));

    schemaRegistryClient.startSchemaVersionReview(schemaIdVersion2.getSchemaVersionId());
    SchemaVersionInfo schemaVersionInfoAtReviewState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtReviewState.getStateId().equals(CustomReviewCycleStates.PEER_REVIEW_STATE.getId()));

    schemaRegistryClient.transitionState(schemaIdVersion2.getSchemaVersionId(), CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId(), "Rejected by X".getBytes());
    SchemaVersionInfo schemaVersionInfoAtRejectedState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtRejectedState.getStateId().equals(CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId()));

}
 
Example #6
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
public SchemaVersionInfo getLatestSchemaVersionInfo(String schemaName, Byte stateId) throws SchemaNotFoundException {
    Preconditions.checkNotNull(schemaName, "schemaName can't be null");

    Collection<SchemaVersionInfo> schemaVersionInfos = getAllVersions(schemaName);

    SchemaVersionInfo latestSchema = null;
    if (schemaVersionInfos != null && !schemaVersionInfos.isEmpty()) {
        for (SchemaVersionInfo schemaVersionInfo : schemaVersionInfos) {
            if (stateId == null || schemaVersionInfo.getStateId().equals(stateId)) {
                latestSchema = schemaVersionInfo;
                break;
            }
        }
    }

    return latestSchema;
}
 
Example #7
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
private SchemaVersionInfo fetchSchemaVersionInfo(String schemaName,
                                                 Integer version) throws SchemaNotFoundException {
    LOG.info("##### fetching schema version for name: [{}] version: [{}]", schemaName, version);
    SchemaVersionInfo schemaVersionInfo = null;
    if (SchemaVersionKey.LATEST_VERSION.equals(version)) {
        schemaVersionInfo = getLatestSchemaVersionInfo(schemaName);
    } else {
        List<QueryParam> queryParams = Lists.newArrayList(
                new QueryParam(SchemaVersionStorable.NAME, schemaName),
                new QueryParam(SchemaVersionStorable.VERSION, version.toString()));

        Collection<SchemaVersionStorable> versionedSchemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams);
        if (versionedSchemas != null && !versionedSchemas.isEmpty()) {
            if (versionedSchemas.size() > 1) {
                LOG.warn("More than one schema exists with name: [{}] and version [{}]", schemaName, version);
            }
            schemaVersionInfo = versionedSchemas.iterator().next().toSchemaVersionInfo();
        } else {
            throw new SchemaNotFoundException("No Schema version exists with name " + schemaName + " and version " + version);
        }
    }
    LOG.info("##### fetched schema version info [{}]", schemaVersionInfo);
    return schemaVersionInfo;
}
 
Example #8
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@Test
public void addSchemaVersionToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);

    SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
    SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
    addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
    addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");

    Collection<SchemaVersionInfo> schemaBranch1VersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
    Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
    Assert.assertTrue(masterSchemaVersionInfos.size() == 1);
    Assert.assertTrue(schemaBranch1VersionInfos.size() == 3);

    Long versionsInInitiatedState = schemaBranch1VersionInfos.stream().filter(
            schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
    Long versionsInEnabledState = schemaBranch1VersionInfos.stream().filter(
            schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
    Assert.assertTrue(versionsInInitiatedState == 2);
    Assert.assertTrue(versionsInEnabledState == 1);
}
 
Example #9
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public void authorizeDeleteSchemaMetadata(UserAndGroups userAndGroups,
                                          ISchemaRegistry schemaRegistry,
                                          String schemaMetadataName)
        throws AuthorizationException, SchemaNotFoundException {

    SchemaMetadataInfo smi = schemaRegistry.getSchemaMetadataInfo(schemaMetadataName);
    if(smi == null) {
        throw new SchemaNotFoundException("No SchemaMetadata exists with key: " + schemaMetadataName);
    }

    Authorizer.SchemaMetadataResource schemaMetadataResource = new Authorizer.SchemaMetadataResource(
            smi.getSchemaMetadata().getSchemaGroup(), schemaMetadataName);
    authorize(schemaMetadataResource, AccessType.DELETE, userAndGroups);

    Collection<SchemaBranch> branches = schemaRegistry.getSchemaBranches(schemaMetadataName);
    if(branches != null) {
        for(SchemaBranch branch : branches) {
            SchemaBranch sb = schemaRegistry.getSchemaBranch(branch.getId());
            authorizeDeleteSchemaBranch(userAndGroups, sb, smi);
        }
    }
}
 
Example #10
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteSchemaVersion(SchemaVersionKey schemaVersionKey) throws SchemaNotFoundException, SchemaLifecycleException {
    schemaVersionInfoCache.invalidateSchema(new SchemaVersionInfoCache.Key(schemaVersionKey));

    Response response = runRetryableBlock((SchemaRegistryTargets targets) -> {
        WebTarget target = targets.schemasTarget.path(String.format("%s/versions/%s", schemaVersionKey
                .getSchemaName(), schemaVersionKey.getVersion()));
        try {
            return login.doAction(new PrivilegedAction<Response>() {
                @Override
                public Response run() {
                    return target.request(MediaType.APPLICATION_JSON_TYPE).delete(Response.class);
                }
            });
        } catch (LoginException | ProcessingException e) {
            throw new RegistryRetryableException(e);
        }
    });

    handleDeleteSchemaResponse(response);
}
 
Example #11
Source File: AbstractSnapshotDeserializer.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
protected void doInit(Map<String, ?> config) {
    schemaCache = CacheBuilder.newBuilder()
            .maximumSize(getCacheMaxSize(config))
            .expireAfterAccess(getCacheExpiryInSecs(config), TimeUnit.SECONDS)
            .build(new CacheLoader<SchemaVersionKey, S>() {
                @Override
                public S load(SchemaVersionKey schemaVersionKey) {
                    try {
                        return getParsedSchema(schemaVersionKey);
                    } catch (SchemaNotFoundException | InvalidSchemaException e) {
                       throw new RegistryException(e);
                    }
                }
            });
}
 
Example #12
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 6 votes vote down vote up
public SchemaVersionInfo findSchemaVersionInfoByFingerprint(final String fingerprint) throws SchemaNotFoundException {
    final List<QueryParam> queryParams = Collections.singletonList(new QueryParam(SchemaVersionStorable.FINGERPRINT, fingerprint));
    final List<OrderByField> orderParams = Collections.singletonList(OrderByField.of(SchemaVersionStorable.TIMESTAMP, true));

    final Collection<SchemaVersionStorable> schemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams, orderParams);

    if (schemas.isEmpty()) {
        throw new SchemaNotFoundException(String.format("No schema found for fingerprint: %s", fingerprint));
    } else {
        if (schemas.size() > 1) {
            LOG.warn(String.format("Multiple schemas found for the same fingerprint: %s", fingerprint));
        }

        return schemas.stream()
                .findFirst()
                .get()
                .toSchemaVersionInfo();
    }
}
 
Example #13
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<SchemaVersionInfo> authorizeGetAllVersions(UserAndGroups userAndGroups,
                                                             ISchemaRegistry schemaRegistry,
                                                             Collection<SchemaVersionInfo> versions)
        throws SchemaNotFoundException {

    return authorizeGetEntities(userAndGroups, versions, schemaVersionInfo -> {
        SchemaMetadata sM =
                schemaRegistry.getSchemaMetadataInfo(schemaVersionInfo.getSchemaMetadataId())
                        .getSchemaMetadata();
        String sGroup = sM.getSchemaGroup();
        String sName = sM.getName();
        String sBranch = getPrimaryBranch(schemaRegistry.getSchemaBranchesForVersion(schemaVersionInfo.getId()));

        return new Authorizer.SchemaVersionResource(sGroup, sName, sBranch);
    });

}
 
Example #14
Source File: AbstractSnapshotSerializer.java    From registry with Apache License 2.0 6 votes vote down vote up
@Override
public final O serialize(I input, SchemaMetadata schemaMetadata) throws SerDesException {
    ensureInitialized();

    // compute schema based on input object
    String schema = getSchemaText(input);

    // register that schema and get the version
    try {
        SchemaIdVersion schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Schema registered by serializer:" + this.getClass()));
        // write the version and given object to the output
        return doSerialize(input, schemaIdVersion);
    } catch (SchemaNotFoundException | IncompatibleSchemaException | InvalidSchemaException | SchemaBranchNotFoundException e) {
        throw new RegistryException(e);
    }
}
 
Example #15
Source File: AvroSchemaRegistryClientTest.java    From registry with Apache License 2.0 6 votes vote down vote up
private void _testAvroSerDesGenericObj(Byte protocolId) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    Map<String, Object> config = Maps.newHashMap();
    config.putAll(SCHEMA_REGISTRY_CLIENT_CONF);
    config.put(SERDES_PROTOCOL_VERSION, protocolId);

    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);

    String deviceSchema = AvroSchemaRegistryClientUtil.getSchema("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(deviceSchema, "Initial version of the schema"));
    Assert.assertNotNull(v1);

    Object deviceObject = AvroSchemaRegistryClientUtil.createGenericRecordForDevice();

    byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    Assert.assertEquals(deviceObject, deserializedObj);
}
 
Example #16
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 6 votes vote down vote up
private <T> Collection<T> removeUnauthorizedAndNullEntities(Collection<T> elems,
                                                            EntityFilterFunction<T> filterFunc)
        throws SchemaNotFoundException {
    if(elems == null) {
        return null;
    }
    ArrayList<T> res = new ArrayList<>();
    for(T elem : elems) {
        try {
            T newElem = filterFunc.filter(elem);
            if(newElem != null) {
                res.add(newElem);
            }
        } catch (AuthorizationException e) { }
    }

    return res;
}
 
Example #17
Source File: SchemaRegistryClient.java    From registry with Apache License 2.0 6 votes vote down vote up
private boolean handleSchemaLifeCycleResponse(Response response) throws SchemaNotFoundException, SchemaLifecycleException {
    boolean result;
    int status = response.getStatus();
    if (status == Response.Status.OK.getStatusCode()) {
        result = response.readEntity(Boolean.class);
    } else if (status == Response.Status.NOT_FOUND.getStatusCode()) {
        throw new SchemaNotFoundException(response.readEntity(String.class));
    } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
        CatalogResponse catalogResponse = readCatalogResponse(response.readEntity(String.class));
        if (catalogResponse.getResponseCode() == CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode()) {
            throw new SchemaLifecycleException(new IncompatibleSchemaException(catalogResponse.getResponseMessage()));
        }
        throw new SchemaLifecycleException(catalogResponse.getResponseMessage());

    } else {
        throw new RuntimeException(response.readEntity(String.class));
    }

    return result;
}
 
Example #18
Source File: MockSchemaRegistryClient.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaIdVersion addSchemaVersion(SchemaMetadata schemaMetadata, SchemaVersion schemaVersion, boolean disableCanonicalCheck)
        throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    try {

       return schemaRegistry.addSchemaVersion(schemaMetadata,
                                                      new SchemaVersion(schemaVersion.getSchemaText(),
                                                                        schemaMetadata.getDescription()),
                                                                        disableCanonicalCheck);
    } catch (UnsupportedSchemaTypeException e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: DefaultSchemaRegistry.java    From registry with Apache License 2.0 5 votes vote down vote up
private AggregatedSchemaMetadataInfo buildAggregatedSchemaMetadataInfo(SchemaMetadataInfo schemaMetadataInfo) throws SchemaNotFoundException, SchemaBranchNotFoundException {

        if (schemaMetadataInfo == null) {
            return null;
        }

        List<SerDesInfo> serDesInfos = getSerDesInfos(schemaMetadataInfo.getSchemaMetadata().getName());

        return new AggregatedSchemaMetadataInfo(schemaMetadataInfo.getSchemaMetadata(),
                                                schemaMetadataInfo.getId(),
                                                schemaMetadataInfo.getTimestamp(),
                                                getAggregatedSchemaBranch(schemaMetadataInfo.getSchemaMetadata().getName()),
                                                serDesInfos);
    }
 
Example #20
Source File: SchemaVersionLifecycleStates.java    From registry with Apache License 2.0 5 votes vote down vote up
private static Pair<SchemaVersionLifecycleStateTransition, SchemaVersionLifecycleStateAction>
createDeleteTransitionActionPair(
        Byte sourceStateId) {
    return Pair.of(new SchemaVersionLifecycleStateTransition(sourceStateId,
                                                             DELETED.getId(),
                                                             "Delete",
                                                             "Deletes the schema version"),
                   context -> {
                       try {
                           transitionToDeleteState(context);
                       } catch (SchemaNotFoundException e) {
                           throw new SchemaLifecycleException(e);
                       }
                   });
}
 
Example #21
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 5 votes vote down vote up
public Collection<SchemaVersionInfo> getAllVersions(final String schemaBranchName,
                                                    final String schemaName) throws SchemaNotFoundException, SchemaBranchNotFoundException {

    Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null");

    Collection<SchemaVersionInfo> schemaVersionInfos;
    SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranchName, schemaName);

    schemaVersionInfos = Lists.reverse(getSortedSchemaVersions(schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey))));
    if (schemaVersionInfos == null || schemaVersionInfos.isEmpty())
        schemaVersionInfos = Collections.emptyList();

    return schemaVersionInfos;
}
 
Example #22
Source File: SchemaBranchLifeCycleTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test (expected = SchemaLifecycleException.class)
public void deleteRootSchemaOfBranch() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);

    SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
    addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());

    schemaRegistryClient.archiveSchemaVersion(masterSchemaIdVersion1.getSchemaVersionId());

    schemaRegistryClient.deleteSchemaVersion(masterSchemaIdVersion1.getSchemaVersionId());
}
 
Example #23
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public void authorizeSchemaVersion(UserAndGroups userAndGroups,
                                   ISchemaRegistry schemaRegistry,
                                   SchemaVersionKey versionKey,
                                   AccessType accessType)
        throws AuthorizationException, SchemaNotFoundException {

    authorizeSchemaVersion(userAndGroups,
            schemaRegistry,
            schemaRegistry.getSchemaVersionInfo(versionKey),
            accessType);
}
 
Example #24
Source File: DefaultAuthorizationAgent.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public void authorizeMapSchemaWithSerDes(UserAndGroups userAndGroups,
                                         ISchemaRegistry schemaRegistry,
                                         String schemaMetadataName)
        throws AuthorizationException, SchemaNotFoundException {

    SchemaMetadataInfo smi = schemaRegistry.getSchemaMetadataInfo(schemaMetadataName);
    if(smi == null) {
        throw new SchemaNotFoundException("No SchemaMetadata exists with key: " + schemaMetadataName);
    }

    authorizeSerDes(userAndGroups, AccessType.READ);
    authorizeSchemaMetadata(userAndGroups, smi.getSchemaMetadata(), AccessType.UPDATE);
}
 
Example #25
Source File: SchemaVersionLifecycleManager.java    From registry with Apache License 2.0 5 votes vote down vote up
private SchemaVersionInfo fetchSchemaVersionInfo(Long id) throws SchemaNotFoundException {
    StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(id));

    SchemaVersionStorable versionedSchema = storageManager.get(storableKey);
    if (versionedSchema == null) {
        throw new SchemaNotFoundException("No Schema version exists with id " + id);
    }
    return versionedSchema.toSchemaVersionInfo();
}
 
Example #26
Source File: CustomReviewCycleExecutor.java    From registry with Apache License 2.0 5 votes vote down vote up
private void transitionToState(SchemaVersionLifecycleContext context,
                               SchemaVersionLifecycleState targetState) throws SchemaLifecycleException {
    if (context.getDetails() == null || context.getDetails().length == 0)
        throw new RuntimeException("This state transition should not be triggered through UI");
    context.setState(targetState);
    try {
        context.updateSchemaVersionState();
    } catch (SchemaNotFoundException e) {
        throw new SchemaLifecycleException(e);
    }
}
 
Example #27
Source File: AvroSchemaResolver.java    From registry with Apache License 2.0 5 votes vote down vote up
private Map<String, Schema> collectSchemaTypes(SchemaVersionKey schemaVersionKey,
                                               Map<String, SchemaParsingState> schemaParsingStates)
        throws SchemaNotFoundException, InvalidSchemaException {

    String schemaName = schemaVersionKey.getSchemaName();
    SchemaParsingState schemaParsingState = schemaParsingStates.putIfAbsent(schemaName, SchemaParsingState.PARSING);

    // if it is already parsed then the respective schema types would have been already collected.
    if (SchemaParsingState.PARSED == schemaParsingState) {
        return null;
    }

    // if it is in parsing state earlier and it is visted again then ther eis circular dependency!!
    if (SchemaParsingState.PARSING == schemaParsingState) {
        throw new CyclicSchemaDependencyException("Cyclic dependency of schema imports with schema [" + schemaName + "]");
    }

    // this schema is not yet parsed till now
    if (schemaParsingState == null) {
        Schema.Parser parser = new Schema.Parser();
        Schema schema = parser.parse(getResultantSchema(schemaVersionKey, schemaParsingStates));
        Map<String, Schema> complexTypes = new HashMap<>();
        collectComplexTypes(schema, complexTypes);
        schemaParsingStates.put(schemaName, SchemaParsingState.PARSED);
        return complexTypes;
    }

    throw new IllegalStateException("Schema parsing with schema version " + schemaVersionKey + " is in invalid state!!");
}
 
Example #28
Source File: SchemaVersionLifecycleStatesTest.java    From registry with Apache License 2.0 5 votes vote down vote up
private void checkEnableNotSupported(InbuiltSchemaVersionLifecycleState state,
                                     SchemaVersionLifecycleContext context) throws SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchNotFoundException {
    try {
        state.enable(context);
        Assert.fail(state.getName() + " should not lead to enable state");
    } catch (SchemaLifecycleException e) {
    }
}
 
Example #29
Source File: DefaultAuthorizationAgentTest.java    From registry with Apache License 2.0 5 votes vote down vote up
@Test
public void authorizeGetAllVersions() throws SchemaNotFoundException {
    String user3 = "user3";
    SecurityContext sc3 = new SecurityContextForTesting(user3);

    // Empty array should stay empty
    List<SchemaVersionInfo> versions = new ArrayList<>();
    Collection<SchemaVersionInfo> res = authorizationAgent.authorizeGetAllVersions(AuthorizationUtils.getUserAndGroups(sc3), schemaRegistry, versions);
    assertTrue(res.isEmpty());

    SchemaVersionInfo svi3 = schemaRegistry.getSchemaVersionInfo(siv3);
    versions.add(svi3);
    SchemaVersionInfo svi31 = schemaRegistry.getSchemaVersionInfo(siv31);
    versions.add(svi31);

    List<SchemaVersionInfo> expected = new ArrayList<>(versions);
    // Authorized by p7
    res = authorizationAgent.authorizeGetAllVersions(AuthorizationUtils.getUserAndGroups(sc3), schemaRegistry, versions);
    assertThat(res, is(expected));

    SchemaVersionInfo svi4 = schemaRegistry.getSchemaVersionInfo(siv4);
    versions.add(svi4);
    expected = new ArrayList<>();
    expected.add(svi4);
    String user4 = "user4";
    SecurityContext sc4 = new SecurityContextForTesting(user4);
    // Authorized by p11
    res = authorizationAgent.authorizeGetAllVersions(AuthorizationUtils.getUserAndGroups(sc4), schemaRegistry, versions);
    assertThat(res, is(expected));

    // NOT_FOUND test cases do not exist
}
 
Example #30
Source File: DefaultSchemaRegistry.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<SchemaBranch> getSchemaBranches(String schemaName) throws SchemaNotFoundException {
    Collection<SchemaVersionInfo> schemaVersionInfos = getAllVersions(schemaName);
    return schemaVersionInfos.stream().flatMap(schemaVersionInfo -> {
        try {
            return schemaVersionLifecycleManager.getSchemaBranches(schemaVersionInfo.getId()).stream();
        } catch (SchemaBranchNotFoundException e) {
            throw new RuntimeException(String.format("Failed to obtain schema branch associated with schema name : %s", schemaName),e);
        }
    }).collect(Collectors.toSet());
}