github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/t_sshfp.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // SetTargetSSHFP sets the SSHFP fields.
    10  func (rc *RecordConfig) SetTargetSSHFP(algorithm uint8, fingerprint uint8, target string) error {
    11  	rc.SshfpAlgorithm = algorithm
    12  	rc.SshfpFingerprint = fingerprint
    13  	rc.SetTarget(target)
    14  	if rc.Type == "" {
    15  		rc.Type = "SSHFP"
    16  	}
    17  	if rc.Type != "SSHFP" {
    18  		panic("assertion failed: SetTargetSSHFP called when .Type is not SSHFP")
    19  	}
    20  
    21  	if algorithm < 1 && algorithm > 4 {
    22  		return fmt.Errorf("SSHFP algorithm (%v) is not one of 1, 2, 3 or 4", algorithm)
    23  	}
    24  	if fingerprint < 1 && fingerprint > 2 {
    25  		return fmt.Errorf("SSHFP fingerprint (%v) is not one of 1 or 2", fingerprint)
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  // SetTargetSSHFPStrings is like SetTargetSSHFP but accepts strings.
    32  func (rc *RecordConfig) SetTargetSSHFPStrings(algorithm, fingerprint, target string) error {
    33  	i64algorithm, err := strconv.ParseUint(algorithm, 10, 8)
    34  	if err != nil {
    35  		return fmt.Errorf("SSHFP algorithm does not fit in 8 bits: %w", err)
    36  	}
    37  	i64fingerprint, err := strconv.ParseUint(fingerprint, 10, 8)
    38  	if err != nil {
    39  		return fmt.Errorf("SSHFP fingerprint does not fit in 8 bits: %w", err)
    40  	}
    41  	return rc.SetTargetSSHFP(uint8(i64algorithm), uint8(i64fingerprint), target)
    42  }
    43  
    44  // SetTargetSSHFPString is like SetTargetSSHFP but accepts one big string.
    45  func (rc *RecordConfig) SetTargetSSHFPString(s string) error {
    46  	part := strings.Fields(s)
    47  	if len(part) != 3 {
    48  		return fmt.Errorf("SSHFP value does not contain 3 fields: (%#v)", s)
    49  	}
    50  	return rc.SetTargetSSHFPStrings(part[0], part[1], StripQuotes(part[2]))
    51  }