Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api clients for talking to remote clouds #6651

Merged
merged 7 commits into from
Dec 11, 2017
Merged

Conversation

icewind1991
Copy link
Member

@icewind1991 icewind1991 commented Sep 26, 2017

We're getting more and more places where we need to talk to remote nextcloud instances, instead of each app having to figure out the \stuff for themselves, it would be better to have some common stuff in core.

Todo:

  • figure out a proper way how to test this

@icewind1991 icewind1991 added the 2. developing Work in progress label Sep 26, 2017
@icewind1991 icewind1991 added this to the Nextcloud 13 milestone Sep 26, 2017
}

public function getUser($userId) {
return new User($this->request('get', '/ocs/v1.php/cloud/users/' . $userId));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please use v2.php
  2. this endpoint only works authenticated?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah lol, but you need valid credentials of the same or an admin user on the third party instance...
I think this is just an example, but it's a bad one I wouldn't ship.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this endpoint only works authenticated

Yes

}

public function getCapabilities() {
return $this->request('get', '/ocs/v1.php/cloud/capabilities');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v2.php

* @return string
*/
public function getTwitter() {
return $this->data['twitter'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 12+ only and will throw undefined index when being called against 11

@nickvergessen
Copy link
Member

Good idea to abstract that away 👍

@nickvergessen
Copy link
Member

As for testing, use functional tests and run the same server behind 2 addresses, like its done for the federation tests?

* @param string $user
* @param string $password
*/
public function __construct($user, $password) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should then be added to the stack trace cleaner to not leak passwords in the log file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is for exceptions that are thrown inside the method, since this method won't throw exceptions it's unneeded

@codecov
Copy link

codecov bot commented Oct 2, 2017

Codecov Report

Merging #6651 into master will increase coverage by 2.41%.
The diff coverage is 54.02%.

@@             Coverage Diff              @@
##             master    #6651      +/-   ##
============================================
+ Coverage     50.43%   52.84%   +2.41%     
+ Complexity    24712    22883    -1829     
============================================
  Files          1522     1449      -73     
  Lines         86422    88732    +2310     
  Branches          0     1349    +1349     
============================================
+ Hits          43585    46894    +3309     
+ Misses        42837    41838     -999
Impacted Files Coverage Δ Complexity Δ
lib/private/Remote/Api/ApiFactory.php 0% <0%> (ø) 2 <2> (?)
lib/private/Remote/Api/ApiCollection.php 0% <0%> (ø) 3 <3> (?)
lib/private/Remote/Credentials.php 100% <100%> (ø) 3 <3> (?)
lib/private/Remote/InstanceFactory.php 100% <100%> (ø) 2 <2> (?)
lib/private/Remote/User.php 14.28% <14.28%> (ø) 15 <15> (?)
lib/private/Server.php 82.93% <30%> (+0.31%) 126 <3> (ø) ⬇️
lib/private/Remote/Api/ApiBase.php 52.5% <52.5%> (ø) 10 <10> (?)
lib/private/Remote/Api/OCS.php 53.33% <53.33%> (ø) 15 <15> (?)
lib/private/Remote/Instance.php 90.47% <90.47%> (ø) 13 <13> (?)
lib/private/Template/JSResourceLocator.php 0% <0%> (-50.91%) 22% <0%> (-1%)
... and 953 more

@icewind1991 icewind1991 force-pushed the remote-cloud-client branch 2 times, most recently from c50b453 to 7bd5bd6 Compare October 6, 2017 13:55
@icewind1991 icewind1991 added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Oct 6, 2017
@icewind1991
Copy link
Member Author

Added tests and plugic api,

ready to review @rullzer @nickvergessen

@icewind1991 icewind1991 force-pushed the remote-cloud-client branch 2 times, most recently from d063ff1 to 9587796 Compare October 17, 2017 15:46
@icewind1991
Copy link
Member Author

Please review @rullzer @nickvergessen @schiessle

@nickvergessen
Copy link
Member

Fine by me, although I would like to not have the interfaces in OCP for now, so we can test them in one release before finalizing them.

👍

@icewind1991
Copy link
Member Author

They are added to the public api since they will be part of the migration api which needs to be implemented by apps

$response = $client->options($fullUrl, $options);
break;
default:
throw new \InvalidArgumentException('Invalid method ' . $method);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @throw to the PHPDoc

class ApiCollection implements IApiCollection {
private $instance;
private $credentials;
private $clientService;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type annotations here would be nice :)

use OCP\Remote\IInstance;

class ApiFactory implements IApiFactory {
private $clientService;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type annotations here would be nice :)

/** @var IClientService */
private $clientService;

private $status;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typehint missing

return $this->status;
}
$key = 'remote/' . $this->url . '/status';
$status = $this->cache->get($key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we permanently store the result of this somewhere? Just because otherwise we're prone to downgrade attacks if an adversary blocks the 443 port.

return $status;
}

private function downloadStatus($url) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPDocs would be nice :)


class InstanceFactory implements IInstanceFactory {
private $cache;
private $clientService;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typehints would be nice :)

*/
interface IInstanceFactory {
/**
* @param $url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string?

}

public function getUser($userId) {
return new User($this->request('get', 'cloud/users/' . $userId));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty error-prone, especially since the User class is just accessing the array data. Can we have more issets in the User class and some sanity check on the response?

@icewind1991
Copy link
Member Author

@LukasReschke all done.

It now separately caches if a remote supports https and refuses to downgrade to http

$status = $this->cache->get($key);
if (!$status) {
$response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php');
$protocol = 'https';
if (!$response) {
if ($status = $this->cache->get($httpsKey)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cache in IAppConfig or so 😇

Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
@MorrisJobke MorrisJobke mentioned this pull request Dec 8, 2017
28 tasks
Copy link
Member

@rullzer rullzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets get this in!

@rullzer rullzer merged commit e8acf44 into master Dec 11, 2017
@rullzer rullzer deleted the remote-cloud-client branch December 11, 2017 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants