gitee.com/mysnapcore/mysnapd@v0.1.0/daemon/api_system_recovery_keys.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package daemon
    21  
    22  import (
    23  	"encoding/json"
    24  	"net/http"
    25  
    26  	"gitee.com/mysnapcore/mysnapd/overlord/auth"
    27  	"gitee.com/mysnapcore/mysnapd/overlord/devicestate"
    28  )
    29  
    30  var systemRecoveryKeysCmd = &Command{
    31  	Path:        "/v2/system-recovery-keys",
    32  	GET:         getSystemRecoveryKeys,
    33  	POST:        postSystemRecoveryKeys,
    34  	ReadAccess:  rootAccess{},
    35  	WriteAccess: rootAccess{},
    36  }
    37  
    38  func getSystemRecoveryKeys(c *Command, r *http.Request, user *auth.UserState) Response {
    39  	st := c.d.overlord.State()
    40  	st.Lock()
    41  	defer st.Unlock()
    42  
    43  	keys, err := c.d.overlord.DeviceManager().EnsureRecoveryKeys()
    44  	if err != nil {
    45  		return InternalError(err.Error())
    46  	}
    47  
    48  	return SyncResponse(keys)
    49  }
    50  
    51  var deviceManagerRemoveRecoveryKeys = (*devicestate.DeviceManager).RemoveRecoveryKeys
    52  
    53  type postSystemRecoveryKeysData struct {
    54  	Action string `json:"action"`
    55  }
    56  
    57  func postSystemRecoveryKeys(c *Command, r *http.Request, user *auth.UserState) Response {
    58  
    59  	var postData postSystemRecoveryKeysData
    60  
    61  	decoder := json.NewDecoder(r.Body)
    62  	if err := decoder.Decode(&postData); err != nil {
    63  		return BadRequest("cannot decode recovery keys action data from request body: %v", err)
    64  	}
    65  	if decoder.More() {
    66  		return BadRequest("spurious content after recovery keys action")
    67  	}
    68  	switch postData.Action {
    69  	case "":
    70  		return BadRequest("missing recovery keys action")
    71  	default:
    72  		return BadRequest("unsupported recovery keys action %q", postData.Action)
    73  	case "remove":
    74  		// only currently supported action
    75  	}
    76  	st := c.d.overlord.State()
    77  	st.Lock()
    78  	defer st.Unlock()
    79  
    80  	err := deviceManagerRemoveRecoveryKeys(c.d.overlord.DeviceManager())
    81  	if err != nil {
    82  		return InternalError(err.Error())
    83  	}
    84  	return SyncResponse(nil)
    85  }