@startuml

start
if (Should regen certs or hasn't private key?) is (YES) then
    :Generate CSR and private key;
    :Save private key to file;
    :KeyPEMFile\\
    :Save Agent Token (fingerprint of CSR) to file;
    :AgentTokenFile\\
else (NO)
    :Read private key file;
    :KeyPEMFile\\
    :Generate CSR;
    :Read agent token from file;
    :AgentTokenFile\\
endif

split
    :Server Token/
split again
    :Agent Token/
    note left: It is required
end split

split
    :Send to server;
    fork
        if (Server returns that the agent is already registered?) is (YES) then
            end
        else (NO)
        endif
        split
            :Agent CA cert/
            note right
                Fingerprint of
                this certificate 
                is stored
                in database
            end note
        split again
            :Server CA cert/
        end split
        :Save certs;
        split
            :RootCAFile\\
            detach
        split again
            :CertPEMFile\\
            detach
        end split
    fork again
        :Machine ID/
    end fork

split again
end split

if (Is server token provided?) is (YES) then
    :Ping agent;
else (NO)
endif
end
@enduml