-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ssh): add ssh key management API
support management ssh key via API
- Loading branch information
1 parent
45b618b
commit 4ca8335
Showing
7 changed files
with
171 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sshkey | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/cnrancher/autok3s/pkg/common" | ||
|
||
"github.com/rancher/apiserver/pkg/apierror" | ||
"github.com/rancher/apiserver/pkg/types" | ||
"github.com/rancher/wrangler/pkg/schemas/validation" | ||
) | ||
|
||
func ActionHandlers() map[string]http.Handler { | ||
return map[string]http.Handler{ | ||
"export": http.HandlerFunc(exportHandler), | ||
} | ||
} | ||
|
||
func exportHandler(w http.ResponseWriter, r *http.Request) { | ||
apiContext := types.GetAPIContext(r.Context()) | ||
defer r.Body.Close() | ||
name := apiContext.Name | ||
sshkeys, err := common.DefaultDB.ListSSHKey(&name) | ||
if err != nil { | ||
apiContext.WriteError(apierror.WrapAPIError(err, validation.ServerError, "failed to get sshkey")) | ||
return | ||
} | ||
obj := common.GetAPIObject(*sshkeys[0]) | ||
apiContext.WriteResponse(200, *obj) | ||
return | ||
} | ||
|
||
func Format(request *types.APIRequest, resource *types.RawResource) { | ||
resource.AddAction(request, "export") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package sshkey | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cnrancher/autok3s/pkg/common" | ||
pkgsshkey "github.com/cnrancher/autok3s/pkg/sshkey" | ||
|
||
"github.com/rancher/apiserver/pkg/apierror" | ||
"github.com/rancher/apiserver/pkg/store/empty" | ||
"github.com/rancher/apiserver/pkg/types" | ||
"github.com/rancher/wrangler/pkg/data/convert" | ||
"github.com/rancher/wrangler/pkg/schemas/validation" | ||
) | ||
|
||
type Store struct { | ||
empty.Store | ||
} | ||
|
||
func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject) (types.APIObject, error) { | ||
rtn := common.SSHKey{} | ||
if err := convert.ToObj(data.Object, &rtn); err != nil { | ||
return types.APIObject{}, err | ||
} | ||
if exist, _ := common.DefaultDB.SSHKeyExists(rtn.Name); exist { | ||
return types.APIObject{}, apierror.NewAPIError(validation.Conflict, fmt.Sprintf("sshkey %s already exists", rtn.Name)) | ||
} | ||
|
||
if rtn.GenerateKey { | ||
if rtn.Bits%256 != 0 { | ||
return types.APIObject{}, apierror.NewFieldAPIError(validation.InvalidBodyContent, "bits", "ssh private key bit size should be a multiple of 256") | ||
} | ||
if err := pkgsshkey.GenerateSSHKey(&rtn); err != nil { | ||
return types.APIObject{}, apierror.WrapAPIError(err, validation.ServerError, "failed to generate ssh key") | ||
} | ||
// remove passphrase for response | ||
rtn.SSHPassphrase = "" | ||
} else { | ||
if rtn.SSHKey == "" { | ||
return types.APIObject{}, apierror.NewAPIError(validation.MissingRequired, "ssh private key is required when not generating new ssh key pair") | ||
} | ||
if ok, err := pkgsshkey.NeedPasswordRaw([]byte(rtn.SSHKey)); err != nil { | ||
return types.APIObject{}, apierror.WrapAPIError(err, validation.InvalidBodyContent, "failed to parse ssh private key") | ||
} else if ok && rtn.SSHPassphrase == "" { | ||
return types.APIObject{}, apierror.NewAPIError(validation.MissingRequired, "ssh key passphrase is required when the private is encrypted") | ||
} | ||
if err := pkgsshkey.CreateSSHKey(&rtn); err != nil { | ||
return types.APIObject{}, apierror.WrapAPIError(err, validation.ServerError, "failed to save ssh key") | ||
} | ||
} | ||
|
||
return *common.GetAPIObject(rtn), nil | ||
} | ||
|
||
func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) { | ||
var rtn types.APIObjectList | ||
sshkeys, err := common.DefaultDB.ListSSHKey(nil) | ||
if err != nil { | ||
return rtn, err | ||
} | ||
for _, key := range sshkeys { | ||
key.SSHKey = "" | ||
obj := common.GetAPIObject(*key) | ||
rtn.Objects = append(rtn.Objects, *obj) | ||
} | ||
return rtn, nil | ||
} | ||
|
||
func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) { | ||
rtn, err := s.ByID(apiOp, schema, id) | ||
if err != nil { | ||
return types.APIObject{}, err | ||
} | ||
|
||
return rtn, common.DefaultDB.DeleteSSHKey(id) | ||
} | ||
|
||
func (s *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) { | ||
rtn, err := common.DefaultDB.ListSSHKey(&id) | ||
if err != nil { | ||
return types.APIObject{}, err | ||
} | ||
rtn[0].SSHKey = "" | ||
obj := common.GetAPIObject(rtn[0]) | ||
return *obj, nil | ||
} | ||
|
||
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types.WatchRequest) (chan types.APIEvent, error) { | ||
return common.DefaultDB.Watch(apiOp, schema), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters