4
0

added SystemSSHEnabled

This commit is contained in:
Benjamin Böhmke 2019-12-10 18:57:01 +01:00
parent e795fe0158
commit 1ea1259aad
2 changed files with 29 additions and 0 deletions

View File

@ -33,6 +33,16 @@ func SystemSetRootPassword(password string) error {
return ioutil.WriteFile(systemShadow, []byte(line), os.ModePerm)
}
// SystemSSHEnabled returns true if server is enabled
func SystemSSHEnabled() (bool, error) {
data, err := ioutil.ReadFile(systemDropbearConfig)
if err != nil {
return false, fmt.Errorf("failed to read ssh config: %w", err)
}
return strings.Contains(string(data), "127.0.0.1:22"), nil
}
// SystemEnableSSH server
func SystemEnableSSH() error {
err := ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"\""), os.ModePerm)

View File

@ -34,6 +34,25 @@ func TestSystemSetRootPassword(t *testing.T) {
_ = os.Remove(systemShadow)
}
func TestSystemSSHEnabled(t *testing.T) {
ass := assert.New(t)
_, err := SystemSSHEnabled()
ass.EqualError(err, "failed to read ssh config: open test_dropbear: no such file or directory")
ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("test"), os.ModePerm))
value, err := SystemSSHEnabled()
ass.NoError(err)
ass.False(value)
ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"-p 127.0.0.1:22\""), os.ModePerm))
value, err = SystemSSHEnabled()
ass.NoError(err)
ass.True(value)
_ = os.Remove(systemDropbearConfig)
}
func TestSystemEnableSSH(t *testing.T) {
ass := assert.New(t)