Search in sources :

Example 1 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class TypedPlatformLayerClient method addTags.

public Tags addTags(PlatformLayerKey key, List<Tag> tags) throws PlatformLayerClientException {
    TagChanges changeTags = new TagChanges();
    changeTags.addTags.addAll(tags);
    return changeTags(key, changeTags);
}
Also used : TagChanges(org.platformlayer.core.model.TagChanges)

Example 2 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class ManagedKeystore method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    KeyStore keystore = null;
    boolean dirty = false;
    List<String> keyAliases;
    {
        byte[] data = target.readBinaryFile(path);
        try {
            if (data != null) {
                keystore = KeyStoreUtils.load(data, keystoreSecret);
            } else {
                keystore = KeyStoreUtils.createEmpty(keystoreSecret);
                dirty = true;
            }
            keyAliases = KeyStoreUtils.getKeyAliases(keystore);
        } catch (GeneralSecurityException e) {
            throw new OpsException("Error reading keystore", e);
        } catch (IOException e) {
            throw new OpsException("Error reading keystore", e);
        }
    }
    if (keyAliases.contains(alias)) {
        try {
            Certificate[] existingCertificateChain = keystore.getCertificateChain(alias);
            if (existingCertificateChain == null || existingCertificateChain.length == 0) {
                keyAliases.remove(alias);
            } else {
                boolean remove = false;
                if (key != null) {
                    X509Certificate[] wantCertificateChain = key.getCertificateChain();
                    // is the same
                    if (!Objects.equal(wantCertificateChain[0], existingCertificateChain[0])) {
                        log.warn("Key found, but mismatch on certificate; will remove");
                        remove = true;
                    }
                }
                if (remove) {
                    // TODO: Rename instead??
                    keystore.deleteEntry(alias);
                    dirty = true;
                    keyAliases.remove(alias);
                }
            }
        } catch (KeyStoreException e) {
            throw new OpsException("Error reading from keystore", e);
        }
    }
    if (!keyAliases.contains(alias)) {
        if (key == null) {
            insertSelfSignedKey(keystore);
        } else {
            insertKey(keystore, key);
        }
        dirty = true;
        keyAliases.add(alias);
    }
    if (tagWithPublicKeys != null) {
        List<String> publicKeySigs = Lists.newArrayList();
        try {
            // for (String alias : keyAliases) {
            Certificate[] cert = keystore.getCertificateChain(alias);
            if (cert.length == 0) {
                log.warn("Ignoring zero length certificate chain for: " + alias);
            // continue;
            } else {
                PublicKey certPublicKey = cert[0].getPublicKey();
                String sigString = OpenSshUtils.getSignatureString(certPublicKey);
                publicKeySigs.add(sigString);
            }
        // }
        } catch (GeneralSecurityException e) {
            throw new OpsException("Error reading public keys", e);
        }
        List<String> existingSigs = Tag.PUBLIC_KEY_SIG.find(tagWithPublicKeys.getTags());
        List<String> missing = Lists.newArrayList();
        for (String publicKeySig : publicKeySigs) {
            if (!existingSigs.contains(publicKeySig)) {
                missing.add(publicKeySig);
            }
        }
        if (!missing.isEmpty()) {
            TagChanges tagChanges = new TagChanges();
            for (String add : missing) {
                tagChanges.addTags.add(Tag.PUBLIC_KEY_SIG.build(add));
            }
            platformlayer.changeTags(tagWithPublicKeys.getKey(), tagChanges);
        }
    }
    if (dirty) {
        byte[] data;
        try {
            data = KeyStoreUtils.serialize(keystore, keystoreSecret);
        } catch (GeneralSecurityException e) {
            throw new OpsException("Error serializing keystore", e);
        } catch (IOException e) {
            throw new OpsException("Error serializing keystore", e);
        }
        FileUpload.upload(target, path, data);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PublicKey(java.security.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) TagChanges(org.platformlayer.core.model.TagChanges) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Handler(org.platformlayer.ops.Handler)

Example 3 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class Tagger method handler.

@Handler
public void handler() throws OpsException {
    if (OpsContext.isDelete() || OpsContext.isConfigure()) {
        TagChanges tagChanges = tagChangesProvider.get();
        if (tagChanges != null) {
            log.info("Setting tags on " + platformLayerKey);
            if (OpsContext.isDelete()) {
                // Swap the tags for a removal
                Tags x = tagChanges.addTags;
                tagChanges.addTags = tagChanges.removeTags;
                tagChanges.removeTags = x;
            }
            platformLayer.changeTags(platformLayerKey, tagChanges, null);
        }
    }
}
Also used : TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags) Handler(org.platformlayer.ops.Handler)

Example 4 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class OpenstackInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final OpenstackInstance model = OpsContext.get().getInstance(OpenstackInstance.class);
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = model;
        addChild(instance);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                OpenstackComputeMachine machine = OpsContext.get().getInstance(OpenstackComputeMachine.class);
                TagChanges tagChanges = new TagChanges();
                tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
                tagChanges.addTags.addAll(machine.buildAddressTags());
                return tagChanges;
            }
        };
        instance.addChild(Tagger.build(model, tagChanges));
    }
// Note: We can't bootstrap an instance, because we can't log in to it,
// because the public key is not our service's public key
// if (model.publicPorts != null) {
// for (int publicPort : model.publicPorts) {
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.port = publicPort;
// publicPortForward.backendItem = model;
// kvm.addChild(publicPortForward);
// }
// }
}
Also used : OpsProvider(org.platformlayer.ops.OpsProvider) OpenstackInstance(org.platformlayer.service.cloud.openstack.model.OpenstackInstance) TagChanges(org.platformlayer.core.model.TagChanges) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine)

Example 5 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class OpenstackPublicEndpointController method addChildren.

// @Inject
// ImageFactory imageFactory;
// 
@Override
protected void addChildren() throws OpsException {
    final OpenstackPublicEndpoint model = OpsContext.get().getInstance(OpenstackPublicEndpoint.class);
    OpenstackInstance instance = client.getItem(model.instance, OpenstackInstance.class);
    CloudInstanceMapper instanceMapper;
    {
        instanceMapper = injected(CloudInstanceMapper.class);
        instanceMapper.instance = instance;
        addChild(instanceMapper);
    }
    final EnsureFirewallIngress ingress;
    {
        ingress = injected(EnsureFirewallIngress.class);
        ingress.model = model;
        instanceMapper.addChild(ingress);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                String address = ingress.getPublicAddress();
                if (Strings.isNullOrEmpty(address)) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(address, model.publicPort);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagger);
        Tagger tagInstance = injected(Tagger.class);
        tagInstance.platformLayerKey = null;
        tagInstance.platformLayerKey = model.instance;
        tagInstance.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagInstance);
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) OpenstackPublicEndpoint(org.platformlayer.service.cloud.openstack.model.OpenstackPublicEndpoint) Tagger(org.platformlayer.ops.tagger.Tagger) OpenstackInstance(org.platformlayer.service.cloud.openstack.model.OpenstackInstance) TagChanges(org.platformlayer.core.model.TagChanges)

Aggregations

TagChanges (org.platformlayer.core.model.TagChanges)20 OpsException (org.platformlayer.ops.OpsException)11 OpsProvider (org.platformlayer.ops.OpsProvider)9 Tag (org.platformlayer.core.model.Tag)7 Tags (org.platformlayer.core.model.Tags)5 Tagger (org.platformlayer.ops.tagger.Tagger)5 EndpointInfo (org.platformlayer.core.model.EndpointInfo)4 ItemBase (org.platformlayer.core.model.ItemBase)4 File (java.io.File)3 IOException (java.io.IOException)3 List (java.util.List)3 AddressModel (org.platformlayer.core.model.AddressModel)3 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)3 DirectInstance (org.platformlayer.service.cloud.direct.model.DirectInstance)3 PublicKey (java.security.PublicKey)2 PlatformLayerClient (org.platformlayer.PlatformLayerClient)2 Command (org.platformlayer.ops.Command)2 Handler (org.platformlayer.ops.Handler)2 Machine (org.platformlayer.ops.Machine)2 NetworkAddressPoolAssignment (org.platformlayer.ops.pool.NetworkAddressPoolAssignment)2