<commit_before># This module defines a systemd service that sets the SSH host key and
# authorized client key and host name of virtual machines running on
# Amazon EC2, Eucalyptus and OpenStack Compute (Nova).

{ config, lib, pkgs, ... }:

with lib;

{
  config = {

    systemd.services.apply-ec2-data =
      { description = "Apply EC2 Data";

        wantedBy = [ "multi-user.target" "sshd.service" ];
        before = [ "sshd.service" ];

        path = [ pkgs.iproute ];

        script =
          ''
            ${optionalString (config.networking.hostName == "") ''
              echo "setting host name..."
              if [ -s /etc/ec2-metadata/hostname ]; then
                  ${pkgs.nettools}/bin/hostname $(cat /etc/ec2-metadata/hostname)
              fi
            ''}

            if ! [ -e /root/.ssh/authorized_keys ]; then
                echo "obtaining SSH key..."
                mkdir -m 0700 -p /root/.ssh
                if [ -s /etc/ec2-metadata/public-keys-0-openssh-key ]; then
                    cat /etc/ec2-metadata/public-keys-0-openssh-key >> /root/.ssh/authorized_keys
                    echo "new key added to authorized_keys"
                    chmod 600 /root/.ssh/authorized_keys
                fi
            fi

            # Extract the intended SSH host key for this machine from
            # the supplied user data, if available.  Otherwise sshd will
            # generate one normally.
            userData=/etc/ec2-metadata/user-data

            mkdir -m 0755 -p /etc/ssh

            key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' $userData)"
            key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' $userData)"
            if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then
                (umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
                echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
            fi

            key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' $userData)"
            key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' $userData)"
            if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then
                (umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
                echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
            fi
          '';

        serviceConfig.Type = "oneshot";
        serviceConfig.RemainAfterExit = true;
      };

    systemd.services."print-host-key" =
      { description = "Print SSH Host Key";
        wantedBy = [ "multi-user.target" ];
        after = [ "sshd.service" ];
        script =
          ''
            # Print the host public key on the console so that the user
            # can obtain it securely by parsing the output of
            # ec2-get-console-output.
            echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console
            for i in /etc/ssh/ssh_host_*_key.pub; do
                ${config.programs.ssh.package}/bin/ssh-keygen -l -f $i > /dev/console
            done
            echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
          '';
        serviceConfig.Type = "oneshot";
        serviceConfig.RemainAfterExit = true;
      };

  };
}<commit_msg>ec2-data: ensure providing a SSH host key is actually optional<commit_after># This module defines a systemd service that sets the SSH host key and
# authorized client key and host name of virtual machines running on
# Amazon EC2, Eucalyptus and OpenStack Compute (Nova).

{ config, lib, pkgs, ... }:

with lib;

{
  config = {

    systemd.services.apply-ec2-data =
      { description = "Apply EC2 Data";

        wantedBy = [ "multi-user.target" "sshd.service" ];
        before = [ "sshd.service" ];

        path = [ pkgs.iproute ];

        script =
          ''
            ${optionalString (config.networking.hostName == "") ''
              echo "setting host name..."
              if [ -s /etc/ec2-metadata/hostname ]; then
                  ${pkgs.nettools}/bin/hostname $(cat /etc/ec2-metadata/hostname)
              fi
            ''}

            if ! [ -e /root/.ssh/authorized_keys ]; then
                echo "obtaining SSH key..."
                mkdir -m 0700 -p /root/.ssh
                if [ -s /etc/ec2-metadata/public-keys-0-openssh-key ]; then
                    cat /etc/ec2-metadata/public-keys-0-openssh-key >> /root/.ssh/authorized_keys
                    echo "new key added to authorized_keys"
                    chmod 600 /root/.ssh/authorized_keys
                fi
            fi

            # Extract the intended SSH host key for this machine from
            # the supplied user data, if available.  Otherwise sshd will
            # generate one normally.
            userData=/etc/ec2-metadata/user-data

            mkdir -m 0755 -p /etc/ssh

            if [ -s "$userData" ]; then
              key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' $userData)"
              key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' $userData)"
              if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then
                  (umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
                  echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
              fi

              key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' $userData)"
              key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' $userData)"
              if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then
                  (umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
                  echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
              fi
            fi
          '';

        serviceConfig.Type = "oneshot";
        serviceConfig.RemainAfterExit = true;
      };

    systemd.services."print-host-key" =
      { description = "Print SSH Host Key";
        wantedBy = [ "multi-user.target" ];
        after = [ "sshd.service" ];
        script =
          ''
            # Print the host public key on the console so that the user
            # can obtain it securely by parsing the output of
            # ec2-get-console-output.
            echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console
            for i in /etc/ssh/ssh_host_*_key.pub; do
                ${config.programs.ssh.package}/bin/ssh-keygen -l -f $i > /dev/console
            done
            echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
          '';
        serviceConfig.Type = "oneshot";
        serviceConfig.RemainAfterExit = true;
      };

  };
}
{'fraction_non_alphanumeric': 0.13149893584676192, 'fraction_numerical': 0.016266342353298876, 'mean_word_length': 1.58913813459268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4880037', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2035, 'n_words': 514}