<commit_before>package io.cattle.platform.iaas.api.credential;

import io.cattle.platform.api.link.LinkHandler;
import io.cattle.platform.core.constants.CredentialConstants;
import io.cattle.platform.core.model.Credential;
import io.github.ibuildthecloud.gdapi.request.ApiRequest;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

public class SshKeyPemDownloadLinkHandler implements LinkHandler {

    @Override
    public String[] getTypes() {
        return new String[] { "sshKey" };
    }

    @Override
    public boolean handles(String type, String id, String link, ApiRequest request) {
        return CredentialConstants.LINK_PEM_FILE.equalsIgnoreCase(link);
    }

    @Override
    public Object link(String name, Object obj, ApiRequest request) throws IOException {
        if ( obj instanceof Credential ) {
            String secretValue = ((Credential)obj).getSecretValue();
            if ( secretValue == null ) {
                return null;
            }

            byte[] content = secretValue.getBytes("UTF-8");

            HttpServletResponse response = request.getServletContext().getResponse();

            response.setContentLength(content.length);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=mykey.pem");
            response.setHeader("Cache-Control", "private");
            response.setHeader("Pragma", "private");
            response.setHeader("Expires", "Wed 24 Feb 1982 18:42:00 GMT");

            response.getOutputStream().write(content);

            return new Object();
        }

        return null;
    }

}<commit_msg>api: make ssh key file download based on api key name<commit_after>package io.cattle.platform.iaas.api.credential;

import io.cattle.platform.api.link.LinkHandler;
import io.cattle.platform.core.constants.CredentialConstants;
import io.cattle.platform.core.model.Credential;
import io.github.ibuildthecloud.gdapi.context.ApiContext;
import io.github.ibuildthecloud.gdapi.id.IdFormatter;
import io.github.ibuildthecloud.gdapi.request.ApiRequest;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

public class SshKeyPemDownloadLinkHandler implements LinkHandler {

    @Override
    public String[] getTypes() {
        return new String[] { "sshKey" };
    }

    @Override
    public boolean handles(String type, String id, String link, ApiRequest request) {
        return CredentialConstants.LINK_PEM_FILE.equalsIgnoreCase(link);
    }

    @Override
    public Object link(String name, Object obj, ApiRequest request) throws IOException {
        if ( obj instanceof Credential ) {
            String secretValue = ((Credential)obj).getSecretValue();
            if ( secretValue == null ) {
                return null;
            }

            byte[] content = secretValue.getBytes("UTF-8");

            HttpServletResponse response = request.getServletContext().getResponse();

            response.setContentLength(content.length);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + getFilename((Credential)obj, request));
            response.setHeader("Cache-Control", "private");
            response.setHeader("Pragma", "private");
            response.setHeader("Expires", "Wed 24 Feb 1982 18:42:00 GMT");

            response.getOutputStream().write(content);

            return new Object();
        }

        return null;
    }

    protected String getFilename(Credential cred, ApiRequest request) {
        String name = cred.getName();

        if ( name != null ) {
            return name.trim().replaceAll("[^a-zA-Z0-9]", "_") + ".pem";
        } else {
            IdFormatter formatter = ApiContext.getContext().getIdFormatter();
            Object id = formatter.formatId(cred.getKind(), cred.getId());

            return "id_rsa_" + id + ".pem";
        }
    }

}
{'fraction_non_alphanumeric': 0.11177644710578842, 'fraction_numerical': 0.006986027944111776, 'mean_word_length': 2.9150390625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401698', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1057, 'n_words': 225}